IPhone: transition to the next uitextfield in a uitableview, how?

In my iPhone project, I'm using UITableview with UITableViewCells containing UITextfields. In many applications, I saw that you can use the following button to go to the next text field in the next cell. What is the best way to do this?

My idea is to get the indexPath of the cell with the text field that is being edited, and then get the next cell cellRowAtIndexPath. But how can I get the indexPath of the cell that I am currently editing?

Thank!

+5
source share
2 answers
  • Store instance references UITextFieldin a table view.
  • UITextField.
  • , "Return" "Next" "Done": [finalTextField setReturnKeyType:UIReturnKeyDone];

UITextField -textFieldShouldReturn: :

- (BOOL) textFieldShouldReturn:(UITextField *)tf {
    switch (tf.tag) {
        case firstTextFieldTag:
            [secondTextField becomeFirstResponder];
            break;
        case secondTextFieldTag:
            [thirdTextField becomeFirstResponder];
            break;
        // etc.
        default:
            [tf resignFirstResponder];
            break;
    }
    return YES;
}
+8

, UITextField UITableViewCell,

UITableViewCell *cell;
UITextField *textField;
...
textField.tag = kTAG_TEXTFIELD;
[cell.contentView addSubview:textField];
...

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
  if([textField.superView.superView isKindOfClass:[UITableViewCell class]]) {
    UITableViewCell *cell = (UITableViewCell *)textField.superView.superView;
    NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
  }
  ...

UITextField

NSIndexPath *indexPathNextRow = [NSIndexPath indexPathForRow:(indexPath.row+1) inSection:indexPath.section];
UITableViewCell *cellNextRow = (UITableViewCell *)[self.myTableView cellForRowAtIndexPath:indexPathNextRow];
UITextField *textFieldNextRow = (UITextField *)[cellNextRow.contentView viewWithTag:kTAG_TEXTFIELD];

, !

0

All Articles