Why am I getting an NSRangeException when I request my table view to scroll up?

I am using a subclassed tableView made by someone else, and it comes with a method loadObjectsthat causes the tableView to overflow its cells, however it does not report that the table view returns to the beginning after reloading. I tried to implement this function with:

- (void)loadObjects{
    [super loadObjects];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

However, as soon as it is called loadObjects(when the table view wants to load first), I get the following exception:

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

I passed a line that I wrote that scrolls the view of the table to the top to another user method, which is called whenever I click the button, and everything works fine, so there is a problem with my rewriting loadObjects. Does anyone understand the exception that was thrown?

+4
source share
2 answers

Check table reload before calling scrollToRowAtIndexPath.

[self.tableView reloadData];
+3
source

As some of the commentators noted, you will see this error when you try to scroll to a pointer path that does not exist (according to the results of numberOfSectionsInTableView and numberOfRowsInSection).

, , . , , , ( ) (, , ).

+3

All Articles