views:

267

answers:

4

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.

A: 

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

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1] 
                      atScrollPosition:UITableViewScrollPositionNone 
                              animated:NO];
Laurent Etiemble
That call is deprecated in 4.0. I know we can't discuss it, so without breaking NDA - how can I specify a section if I dont contain any?
Sheehan Alam
@Sheehan: I can't see the deprecation warning in XCode, in the headers or in the documentation. Are you sure the call is deprecated ?
Laurent Etiemble
Sorry I am wrong its not deprecated. I did get this error:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'
Sheehan Alam
A: 

I recommend you create a section to store your cells in. This section can have no header so that it's not visually distinguishable, and what this gains you is the ability to not have to fight against the intended API pathways.

fbartho
A: 

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

pollyp
A: 

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:.

Jay Goodman Tamboli