开发者

UITableView does not scroll to cell

开发者 https://www.devze.com 2023-01-01 14:48 出处:网络
I am trying to scroll my tableview to the 2nd cell: [self.tableView scrollToR开发者_高级运维owAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]

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

[self.tableView scrollToR开发者_高级运维owAtIndexPath:[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.


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


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

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1] 
                      atScrollPosition:UITableViewScrollPositionNone 
                              animated:NO];


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消