ios - 如何从 TextField 委托函数之一打印包含 UITextField 的 TableView 单元格的 indexPath?
问题描述
这是我生成自定义单元格的地方,其中存储了一个名为“indexPath”的属性。此外,每个单元格都包含一个 UITextField。文本字段委托功能如下。这两个函数都存在于同一个 ViewController 中。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! BuyerCell
cell.nameField.delegate = self
cell.indexPath = indexPath
cell.nameField.text = potentialBuyers[indexPath.row]
return cell
}
这是 TextField 委托函数。我需要从委托函数中打印单元格的 indexPath 。
func textFieldDidEndEditing(_ textField: UITextField) {
print("called textFieldDidEndEditing")
//I need to print the cell's indexPath here
}
解决方案
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! BuyerCell
cell.nameField.delegate = self
cell.indexPath = indexPath
cell.nameField.text = potentialBuyers[indexPath.row]
// add tag for the textField
cell.nameField.tag = indexPath.row
return cell
}
现在在 textFieldDelegate 方法中像这样获取您的单元格和 cell.indexPath
func textFieldDidEndEditing(_ textField: UITextField) {
print("called textFieldDidEndEditing")
let cell = tableView.cellForRow(at: IndexPath(row: textField.tag, section: 0)) as! BuyerCell
print(cell.indexPath)
}
推荐阅读
- c++ - 从 field_buffer 返回的 char* 指向的缓冲区大小
- postman - 如何在 Postman 中放置两个 Bearer 密钥?
- html - Google Chrome 扩展程序不显示图标
- java - 将数字格式的时间戳转换为 Java 中的 LocalDateTime
- javascript - 获取js中属性的原始值
- c++ - 用顶点缓冲对象绘制平面
- firebase - 如何向 Firebase 受众发送电子邮件营销活动?
- apex - Visualforce 页面上用于导航的按钮不起作用
- powershell - 尝试使用 PowerShell Set-AzEventHub cmdlet 设置 Azure 事件中心捕获设置时出错
- c++ - 将 CMake 和 Clang++ 与 C++20 模块支持一起使用