How to get cell indexpath in uitextfield Delegate Methods?

后端 未结 11 985
南旧
南旧 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:15

    I find this answer searching how can I find the index path of a cell with inside a UITextField.

    So, thanks to the answer above, I put my code here, hoping might be usefull.

    - (void)searchSelectedIndexPath:(UIView*)view {
        // This allow to find selected index path for a table view cell with a text field inside.
        for (UIView* subview in view.subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                if ([view isFirstResponder]) {
                    UIView *cell = view;
                    while (cell && ![cell isKindOfClass:[UITableViewCell class]]) {
                        cell = cell.superview;
                    }
                    self.selectedIndexPath = [self.tableView indexPathForRowAtPoint:cell.center];
                    return;
                }
            }
            [self searchSelectedIndexPath:subview];
        }
    }
    

    In this way, when keyboard notification will be raise:

    - (void)keyboardDidShow:(NSNotification*)notification {
        [self searchSelectedIndexPath:self.tableView];
    }
    

提交回复
热议问题