views:

332

answers:

1

In my iPhone project I'm using a UITableview with UITableViewCells containing UITextfields. I have seen in many apps that it is possible to use a next button to jump to the next textfield in the next cell. What is the best way to accomplish this?

My idea is to get the indexPath of the cell with the textfield that is being editing and then get the next cell by cellForRowAtIndexPath. But how can I get the indexPath of the cell I'm currently editing?

Thanks!

+2  A: 
  1. Keep references to the UITextField instances in your table view.
  2. Assign unique tag values to your UITextField instances.
  3. In your last text field, you might set its Return key type, which changes the keyboard's Return key label from "Next" to "Done": [finalTextField setReturnKeyType:UIReturnKeyDone];

In the UITextField delegate method -textFieldShouldReturn:, walk through the responders:

- (BOOL) textFieldShouldReturn:(UITextField *)tf {
    switch (tf.tag) {
        case firstTextFieldTag:
            [secondTextField becomeFirstResponder];
            break;
        case secondTextFieldTag:
            [thirdTextField becomeFirstResponder];
            break;
        // etc.
        default:
            [tf resignFirstResponder];
            break;
    }
    return YES;
}
Alex Reynolds
Thanks! But I'm just keeping track of the current textfield as there is many textfields. Can I in some way get the next textfield without using tags?
Mrbiggerm
Sure, just keep references to them, `UITextField *firstTextField`, or use an array, etc. But tags are probably the fastest way to identify the delegate's text field.
Alex Reynolds
But if I don't want to keep track of all textfields or tags? Can I in some way get the current cell that the currently editing textfield is in and then get the indexpath?
Mrbiggerm
If you have selection enabled on the cell, you might be able to do something with the `-tableView:didSelectRowAtIndexPath:indexPath` method but that has nothing to do with a text field, per se.
Alex Reynolds