uitableview - 如何在 TableViewCell 类中的 TextField 委托方法上获取单元格索引
问题描述
请查看下面的代码。我想在文本字段开始编辑时获取单元格索引。
class SingleLineText: UITableViewCell, UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let cell: SingleLineText = textField.superview!.superview as! SingleLineText
let table: UITableView = cell.superview as! UITableView
let textFieldIndexPath = table.indexPath(for: cell)
print(textFieldIndexPath as Any)
return true
}
}
解决方案
在 cellForRowAt 方法中只需添加:
youCell.textField.tag = indexPath.row
并像这样使用它:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let indexPath: IndexPath = IndexPath(row: textField.tag, section: 0)
//here is your indexPath
return true
}
}
推荐阅读
- c++ - 在c ++中围绕中心元素构造镜像向量
- php - PHP:将全局变量传递给函数?
- python - 如何根据列标签获取数据框的设置差异?
- elasticsearch - 弹性搜索。根据上一个结果进行搜索的无痛脚本
- javascript - 在初始页面加载时在 Next.js 中获取整个应用程序的全局数据
- r - R从矩阵中得到回归线
- string - 将 unicode 转换为“java”
- python - 如何通过gspread清除谷歌表格中的范围
- charts - Allow users turn colour breakdown / legend on and off on a bar chart
- android - 带有垂直放置文本的浮动操作按钮