开发者

Scrolling UITableView to valid index path throws error

开发者 https://www.devze.com 2023-01-04 12:15 出处:网络
Why would the following code throw this error? if ([self.tableView.dataSource numberOfSectionsInTableView:self.tableView] > 0 && [self.tableView.dataSource tableView:self.tableView numb开发

Why would the following code throw this error?

if ([self.tableView.dataSource numberOfSectionsInTableView:self.tableView] > 0 && [self.tableView.dataSource tableView:self.tableView numb开发者_运维百科erOfRowsInSection:0] > 1) {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

throws

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

It's pretty early in the morning, but I'm stumped.


Try to call [self.tableView reloadData] just before the lines that you post.

I bet that the datasource is ready after the table is rendered and you call the scrollTo... method before rendering the table with the data in the datasource...

0

精彩评论

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