I am in a UITableViewController and I have textfields inside cells. When the user clicks on the textfield, I implement the UITextFieldDelegate and in method textFieldDidBeganEditing I determine the index of the cell and scroll to that position. It works perfectly for all cells expect for the last 4-5 cells. What am I doing wrong? Thanks.
-
Do you have separate references to your textFields or are you adding it in your cellForRowAtIndexPath: method? – Schoob Jun 15 '11 at 07:06
-
Please supply your implementation for determining the indexPath of the cell. – Schoob Jun 15 '11 at 07:13
3 Answers
scrollToRowAtIndexPath: method scrolls the cell to UITableViewScrollPositionTop or UITableViewScrollPositionMiddle only if the tableView's contentSize is large enough to bring the cell to those positions. If the cell you are trying to scroll to top or middle is the last cell or among the last few cells, it can not be scrolled to middle or top. In these situations you need to calculate the contentOffset manually for those cells.
Edit - Calculating the contentOffset:
In order to calculate the contentOffset use the method as specified by @Schoob. Put the following code in your textFieldDidBeginEditing method.
CGPoint origin = textField.frame.origin;
CGPoint point = [textField.superview convertPoint:origin toView:self.tableView];
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGPoint offset = tableView.contentOffset;
// Adjust the below value as you need
offset.y += (point.y - navBarHeight);
[tableView setContentOffset:o animated:YES];

- 51,274
- 23
- 147
- 178
-
Thanks for the answer. I think that's the reason, but can you give an example on how I would calculate the new content offset? Thanks again! – user635064 Jun 15 '11 at 08:08
-
1why use offset.y += (point.y - navBarHeight) rather than offset.y = (point.y - navBarHeight). – HamasN Oct 18 '13 at 06:54
I found the key to using scrollToRowAtIndexPath was to make sure to call this after the view has been presented. I.e. pushed on to a navigationcontroller or presented modally.
My working code goes like this
Calling code
MyViewController *cont = [[MyViewController alloc] initWithMedication:medication];
cont.sectionToShow = 1;
[self.navigationController pushViewController:cont animated:YES];
[cont release];
Viewcontroller code inside viewWillAppear
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.sectionToShow];
[cont.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
If I did it any other way there were always edge cases where it didn't work. I guess we want to scroll late in the presentation cycle.

- 31
- 3
-
Yep, my strategy of calling -scrollToRowAtIndexPath from -viewDidLoad is not gonna work. – Joshua J. McKinnon Sep 05 '12 at 11:56
This is what you should be using when trying to determine the indexPath of a view/textField added on a UITableViewCell
CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
Then use point with
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];

- 1,688
- 1
- 14
- 18
-
The question was about how to scroll the last few rows. Using this technique to get the index path is fine, but the problem still exists – Paul Heller Dec 19 '12 at 15:02