【问题标题】:How to get Cell Index on TextField Delegate Method in TableViewCell Class如何在 TableViewCell 类中的 TextField 委托方法上获取单元格索引
【发布时间】:2018-07-24 07:23:28
【问题描述】:

请查看下面的代码。我想在文本字段开始编辑时获取单元格索引。

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
    }
}

【问题讨论】:

  • 似乎是什么问题?
  • 在这一行崩溃让单元格:SingleLineText = textField.superview!.superview as!单行文本
  • 不要强制解包选项。同时显示您的SingleLineText 完整代码。或者至少显示视图层次结构的代码。

标签: uitableview swift3


【解决方案1】:

在 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
    }
}

【讨论】:

    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多