【发布时间】: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