【问题标题】:Check textField in each row of tableView查看tableView每一行的textField
【发布时间】:2015-08-20 18:06:21
【问题描述】:

情况是这样的: 我有一个 3 行的 tableView。在这个 tableView 中,有一个带有 textField 的 tableVIewCell 原型。此 textField 用于 tableView 的每一行(不同)。 我的问题是:如何检查每个 textField 是否为空?我猜是一个 If 语句,但我不明白如何分别检查所有文本字段。有人可以帮帮我吗?

我的 tableView 的一个例子: http://imagizer.imageshack.us/a/img540/2130/UsUNO3.png

【问题讨论】:

    标签: ios swift uitableview uitextfield is-empty


    【解决方案1】:

    过去我必须这样做,这是我使用的方法:

    for index in 0...(cellCount - 1) {
    
                var indexpath = NSIndexPath(forRow: index, inSection: 0)
                if let cellAtIndex = tableView.cellForRowAtIndexPath(indexpath) as? ContestantsTableViewCell {
                    var newContestantString = cellAtIndex.contestantNameField.text
    
                    if !newContestantString.isEmpty {
                        contestants.append(newContestantString)
                    }
                }
            }
    

    我只是使用cellForRowAtIndexPath 获取索引路径中的单元格,然后通过将其转换为我的自定义类型从单元格中获取属性。

    【讨论】:

      【解决方案2】:

      如果您知道始终只显示 3 个单元格,那么您可以保持对每个单元格和每个单元格文本字段的引用,并在需要时检查长度。

      【讨论】:

        【解决方案3】:

        可以使用 indexpath.row(Tableview) 在每一行中检查您的文本字段

        if(indexpath.row == 0){
            if(cell.textfield.text == "")
            {
              //1st textfiled empty
            }
        
        }
        else if(indexpath.row == 1){
          if(cell.textfield.text == "")
            {
              //2nd textfiled empty
            }
        }
        else if(indexpath.row == 2){
         if(cell.textfield.text == "")
            {
              //3rd textfiled empty
            }
        }
        

        【讨论】:

        • 这可行,但我们如何将单元格中的所有文本字段文本附加到数组中?
        【解决方案4】:

        好的,您可以创建如下函数。您可以将此函数用于任意数量的单元格,不要使用if else 条件。您还可以将此函数用作文本字段验证。

        func getCellsDataOn() -> Bool {
                for section in 0 ..< self.tableView.numberOfSections {
                    for row in 0 ..< self.tableView.numberOfRows(inSection: section) {
                        let indexPath = NSIndexPath(row: row, section: section)
                        let cell = self.tableView.cellForRow(at: indexPath as IndexPath) as! YourCell
                        if (cell.txtField!.text! != "") {
                            print("Not Empty")
                            //Handle your function 
                        }else {
                            print("Empty")
                            return false
                        }
                    }
                }
                return true
            }
        

        希望对您有所帮助,谢谢。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-11
          • 1970-01-01
          • 2020-03-20
          相关资源
          最近更新 更多