我必须到达分机中的文本字段才能更新文本,但无法调用分机。这个文本字段是我的自定义单元格的一部分,我在 TableView 中使用它。我必须在扩展中到达此文本字段并更改文本。我怎样才能得到这个?
此代码来 self 的表格 View ,我想更改选择器 View 扩展中的 dateTextField 文本。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell", for: indexPath) as! TextFieldCell
let datePicker = UIPickerView()
datePicker.delegate = self
cell.dateTextField.inputView = datePicker
datePicker.backgroundColor = .white
cell.dateTextField.text = secilenTarih
return cell
}
这是我的选择器 View 扩展:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
secilenTarih = tarihArray[row]
// In here I want to access dateTextField.
}
最佳答案
您需要将标签添加到选择器,然后您可以从标签访问您的单元格,然后执行您想要的任何操作。
datePicker.tag = indexpath.row
现在在选择器委托(delegate)中,您可以像这样获取单元格
let path = IndexPath.init(row: pickerView.tag, section: 2)
let cell = tableView.cellForRow(at: path)
现在您可以像这样更改文本字段中的值
cell.dateTextField.text = "your value"
关于ios - 如何访问扩展类中自定义单元格的文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58268977/