1

The cells on my UITableViewController have a textField that can be edited. When I edit the textField I need to scroll the tableview so that the row with the current field is above the keyboard. This is the code I am using:

-(void)textViewDidBeginEditing:(UITextView *)textView
{   
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];

    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell]
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:YES];
}

The problem is that it doesn't work for the last couple of cells in my tableview. I am guessing that it has something to do with the contentSize of the tableview. Is there a proper way to adjust the offset of the tableview so that the last cells will scroll above the keyboard?

denvdancsk
  • 3,033
  • 2
  • 26
  • 41

1 Answers1

5

Instead if

[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];

you can use

[self.tableView setContentOffset:CGPointMake(0, CGRectGetMinY(cell.frame)) animated:YES];

This will scroll you selected cell to the top of screen (like UITableViewScrollPositionTop) no matter did you tap on first or the last cell. Controlling ContentOffset you can set where selected cell should be positioned.

Igor
  • 1,332
  • 10
  • 15
  • not working in iOS 7 for the first time. But when I Pop Back it works. Using auto layout any idea? – Zeeshan Jun 22 '14 at 20:11