ios - 如何访问扩展类中自定义单元格的文本字段?

标签 ios swift xcode

我必须到达分机中的文本字段才能更新文本,但无法调用分机。这个文本字段是我的自定义单元格的一部分,我在 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/

相关文章:

ios - Storyboard未在 Xcode 11 中删除

ios - 使用方法/函数时最好的方法是什么

json - 在 Swift 中使用来自 Rest API 的数组填充 UITableview

ios - swift : My Google Maps View isn't showing up

iphone - 如何哈希/取消哈希数据文件?

iOS - 添加 Firebase 消息时出现重复符号(与 Google Analytics 冲突)

ios - 问题子类化 UIVisualEffect

ios - 动态 tableViewCell 中 imageView 的长宽比

objective-c - 我如何在 Xcode 中为这条线创建橡皮擦?

iphone - 重置 Facebook 权限 iOS SDK