iosuitableviewuitextfielduitextfielddelegate

Trying to get index of active cell from textField inside custom cell of UITableView in iOS


I have a UITableView in my application where I have custom UITableViewCell which contains a UITextField. What I would like to do is get the correct index of the row when I select a particular textfield from that row.

Unfortunately, I am only able to get the correct index of the row if I actually click on the row, and not when I select the textfield itself. I am implementing the <UITextFieldDelegate>, and when I select a particular UITextField, I call the method:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    int rowTest = [_table indexPathForSelectedRow].row;
    int rowTest1 = [_cell.tireCodeField tag];
    
    NSLog(@"the current row is: %d", rowTest1);
    NSLog(@"and this row is: %d", rowTest);  

    return YES;
    
}

The problem is that the value for the row that I am getting is from whenever the method:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   ...

}

gets called. It is as if there is a disconnect between the UITextField and the row in the table it resides in.

Is there a way for me to get the index of the row by selecting the UITextField that resides within it, instead of selecting the row itself?


Solution

  • The way this is usually done, is to give the text field a tag equal to the indexPath.row, or if you have multiple sections, some mathematical combination of the section and row (like 1000*indexPathSection + indexPath.row).