Get indexPath of UITextField in UITableViewCell with Swift

前端 未结 4 1144
庸人自扰
庸人自扰 2021-02-04 08:26

So, I\'m building a Detail View Controller App that presents a Table with a two-part cell: the label and the Text Field.

I\'m trying to retrieve the Text Field value and

4条回答
  •  醉话见心
    2021-02-04 09:08

    While the currently accepted answer might work, it assumes a specific view hierarchy, which is not a reliable approach since it is prone to change.

    To get the indexPath from a UITextField that is inside a cell, it's much better to go with the following:

    func textFieldDidEndEditing(textField: UITextField!){
        let pointInTable = textField.convert(textField.bounds.origin, to: self.tableView)
        let textFieldIndexPath = self.tableView.indexPathForRow(at: pointInTable)
        ...
    }
    

    This will continue to work independent of eventual changes to the view hierarchy.

提交回复
热议问题