如何获取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
}
}

最新更新