How to get cell indexpath in uitextfield Delegate Methods?

后端 未结 11 984
南旧
南旧 2020-12-08 22:56

I have two textfields in a custom cell how to get the indexpath value of Tableview cell in textfield delegate methods I want to get the input value from user and save it to

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 23:02

    A more dynamic solution (no hardcoded superview levels and same code for different iOS versions).

    Further, indexPathForCell: will not work if the cell is not visible, therefore I use indexPathForRowAtPoint: as workaround.

    //find the UITableViewcell superview
    UIView *cell = textField;
    while (cell && ![cell isKindOfClass:[UITableViewCell class]])
        cell = cell.superview;
    
    //use the UITableViewcell superview to get the NSIndexPath
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];
    

提交回复
热议问题