简体   繁体   中英

Scrolling UITableView to valid index path throws error

Why would the following code throw this error?

if ([self.tableView.dataSource numberOfSectionsInTableView:self.tableView] > 0 && [self.tableView.dataSource tableView:self.tableView numberOfRowsInSection: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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM