ios - Swift - 如何识别哪个自定义 tableview 单元格已被修改?

标签 ios swift uitableview

我有三种自定义单元格类型 - 一种带有文本字段的单元格,另一种带有日期字段,另一种带有选择器字段。

我在这里捕获数据变化:

 func textFieldDidEndEditing(_ textField: UITextField) {
        print("TextField did end editing method called")

        switch segmentedControl.selectedSegmentIndex {
        case 0:
            currentItem.setObject(itemName, forKey: "itemName")
            .....
            currentItem.setObject(serialNumber, forKey: "serialNumber")

            break

        case 1:
            currentItem.setObject(username, forKey: "username")

            currentItem.setObject(email, forKey: "email")
      ...
            break

“currentItem”是一个 CKRecord。

这是错误的。字段(itemName、serialNumber 等)是字符串。它们使用云数据在 ViewDidLoad 中设置。

如何正确更新这些字段

提前致谢

最佳答案

正如 Subin K Kuriakose 在评论中所说,您应该使用 UITextFieldtag 属性来确定它是哪个文本字段。

您需要在创建文本字段单元格时设置tag 属性。只需在某处保留一个变量并在每次创建文本字段时递增它,并将 tag 设置为该变量:

textFieldCount += 1
myCustomCell.textField.tag = textFieldCount

类似的东西,你明白了。

现在表格 View 中的每个文本字段都有一个唯一的标签。在 textFieldDidEndEditing 中,您检查标签:

switch textField.tag {
    case 1:
        // it's the first text field!
    case 2:
        // it's the second text field!
    case 3:
        // it's the third text field!
    default:
        break
}

很简单!

关于ios - Swift - 如何识别哪个自定义 tableview 单元格已被修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036783/

相关文章:

ios - - (UITableViewCell *)tableView :(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called

ios - 在 iOS 中使用 map 进行转弯导航

ios - UISearchController 使用 Swift 2 在另一个数组中搜索数组项

ios - 与 Interface Builder 连接,但 UITableView 为零?

ios - UITableView 及其在 xib 文件中的单元格

ios - 删除后正在观察通知

ios - 转至 TabBar Controller

ios - 在 swift 中使用 bridgeToObjectiveC()

ios - Swift firebase 推送通知无法以零错误工作?

json - 是否可以使用 JSONDecoder 解码附加参数?