Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView does not scroll to cell

I am trying to scroll my tableview to the 2nd cell:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] 
                                  atScrollPosition:UITableViewScrollPositionNone 
                                           animated:NO];

I get the error:

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: section (1) beyond bounds (0).

'

My tableview has 30 cells that are appearing with no sections.

like image 205
Sheehan Alam Avatar asked May 26 '10 06:05

Sheehan Alam


3 Answers

When I got the same error message with a one-section table, I fixed it by creating a NSIndexPath with both a row and section:

NSIndexPath *ip = [NSIndexPath indexPathForRow:itemCurrentlyPlaying inSection:0];

It's not easy to find, but the relevant convenience constructor is documented here:

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSIndexPath_UIKitAdditions/Reference/Reference.html

like image 67
pollyp Avatar answered Oct 20 '22 14:10

pollyp


If you have no sections, then you can try the indexPathWithIndex: class constructor:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1] 
                      atScrollPosition:UITableViewScrollPositionNone 
                              animated:NO];
like image 3
Laurent Etiemble Avatar answered Oct 20 '22 12:10

Laurent Etiemble


Like pollyp, I was able to use indexPathForRow:inSection:. I'm calling that from my viewWillAppear:.

It looks like your error message is saying that there isn't even one section. Are you perhaps calling scrollToRowAtIndexPath:atScrollPosition:animated: before the table data is loaded (e.g. in viewDidLoad)? Try calling it later, in viewWillAppear:.

like image 2
Jay Goodman Tamboli Avatar answered Oct 20 '22 13:10

Jay Goodman Tamboli