1

I am creating a form with textfields in a tableview controller. I am trying to achieve 2 things here when the return key is pressed after entering value for a field:

1) Move cursor to next field 2) Scroll the tableview up

My code is working for moving the cursor to the next field but the scroll part is not working. Could you let me know what I am missing here. I researched similar issues on stack overflow and am following suggestions from them but still facing issues. Appreciate your inputs.

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

    if ([textField isEqual:self.firstName]){
    //Move cursor to next field
    [self.lastName becomeFirstResponder];

    //scroll    
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.lastName]){
    //Move cursor to next field
    [self.emailId becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.emailId]){

    //Move cursor to next field
    [self.phoneNumber becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.phoneNumber]){

    //Move cursor to next field
    [self.password becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

// This is the last field hence dismiss keyboard -> This part is working
if ([textField isEqual:self.password]){
    [textField resignFirstResponder];

}

return YES ;

}
Mike G
  • 751
  • 3
  • 12
  • 21
  • Thats because your ContentSize is not Big Enough, Try and increase your UITableView ContentSize. – Bonnie Jul 18 '13 at 05:28

4 Answers4

7

For scrolling the tableview up as per textfield's focus try this:

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
    [yourTableView scrollToRowAtIndexPath:[yourTableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];   
}

Here you can set scroll position to top by using UITableViewScrollPositionTop

You may wonder why two superviews are used for textfield. One is for UITableViewCellContentView and another is for UITableViewCell so that you can grab the instance of your table's current/focused cell.

It works, we have used this in one of our projects

  • 1
    On iOS7 there is one extra view on the UITableViewCell (as far as I remember, it was for the swipe gesture and delete button). So, your solution will not work on long term. I suggest finding a way to find textfields owner cell on the view controller (eg. iterate through cells and compare textfields) – Yunus Nedim Mehel Mar 28 '14 at 17:14
2

I used the viewcontroller as the data source for the table view. My solution:

  1. Make the UITextField as a public property for your custom UITableViewCell.
  2. Inside cellForRowAtIndexPath, add target to the textfield for UIControlEventEditingDidBegin control event:

    [((YourCustomCell *) cell).textfield addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    
  3. Implement the named method: Find the cell for the textfield and scroll to that cell

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        for ( NSIndexPath *ip in [self.tableview indexPathsForVisibleRows] ){
            UITableViewCell * cell = [self.tableview cellForRowAtIndexPath:ip];
            if  ( [cell isKindOfClass:[YourCustomCell class]]){
    
                if ( [textField isEqual:((YourCustomCell *)cell).textfield] ){
                    [self.tableview selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionTop];
                    [self setContinueButtonEnabled:YES];
                }
            }
        }
    }
    
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
1

You use TKKeyboardAvoidingTableview so no need to mannually scroll tableview it automatically scoll when cursor tap

you set xib in select tableview and set custom class like this

enter image description here

kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
1

Try to use this code it may help you

  CGPoint pt;
    CGRect rc = [textField bounds];
    rc = [textField convertRect:rc toView:scrView];
    pt = rc.origin;
    pt.x = 0;
    pt.y =0;
    [scrView setContentOffset:pt animated:YES];
Romance
  • 1,416
  • 11
  • 21