【问题标题】:Accessing the row number of a UITableViewCell outside of cellForRowAt indexPath访问 cellForRowAt indexPath 之外的 UITableViewCell 的行号
【发布时间】:2020-10-21 06:25:33
【问题描述】:

我正在尝试获取刚刚编辑的 textField 所在的特定行(在 TableViewController 中)(我有一个带有自定义 tableViewCell 的 TableViewController,其中有两个文本字段),所以我可以将 textField 输入保存到我的数组中的相应索引,然后保存到 Realm。 我尝试在我的代码中使用如下所示的 textField.tag,但这导致了错误。 非常感谢,任何帮助将不胜感激!:)

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> NotecardTableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "NotecardCell", for: indexPath) as! NotecardTableViewCell
    cell.termTextField.delegate = self
    cell.defTextField.delegate = self
    
    cell.termTextField.accessibilityIdentifier = "termTextField"
    cell.defTextField.accessibilityIdentifier = "defTextField"
    
    cell.termTextField.tag = indexPath.row
    
    cell.termTextField.text = noteCards?[indexPath.row].term

    return cell
}




func textFieldDidEndEditing(_ textField: UITextField) {
        do {
            try! realm.write {
                if textField.accessibilityIdentifier == "termTextField"{
                    noteCards?[textField.tag].term = textField.text ?? "default"
                }
            }
        }
    }

【问题讨论】:

  • 您采取了正确的方法,即您使用其可访问性标识符属性为每个文本字段命名。但是,您必须根据他们的行给他们一个身份。

标签: ios swift xcode uitableview uitextfield


【解决方案1】:

您应该在单元格中实现UITextFieldDelegate 方法。然后,您可以使用闭包或委托将事件传递回您的表格视图控制器。

例如,使用闭包:


Enum TextFieldIdentifier {
    case term
    case definition
}

class NotecardTableViewCell: UITableViewCell, UITextFieldDelegate {

   var termTextField: UITextField!
   var defTextField: UITextField!

   var updateHandler: ((TextFieldIdentifier,String?)->Void)?

   func textFieldDidEndEditing(_ textField: UITextField) {
        switch textField {
            case termTextField:
                updateHandler?(.term,textField.text
            case defTextField:
                updateHandler?(.definition,textField.text)
            default
                break
        }
    }

那么,在你的cellForRow

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> NotecardTableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "NotecardCell", for: indexPath) as! NotecardTableViewCell

    cell.updateHandler= { [weak self], (fieldType,content) in
        guard let self = self else {
            return
        }

        switch fieldType {
            case .term:
               do {
                   try realm.write {
                       noteCards?[indexPath.row].term = content ?? "default"
                   }
               } catch {
                   print(error)             
               }
            }
            case .definition:
                break
        }
         
    }
    
    cell.termTextField.text = noteCards?[indexPath.row].term

    return cell
}

由于闭包捕获了indexPath.row,因此您不需要做任何棘手的事情来确定您正在处理哪一行。

您也可以使用委托模式。我展示了这种方法in this answer(它用于处理按钮点击,但基本思想是相同的)。

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2021-04-02
    • 2013-09-26
    相关资源
    最近更新 更多