I have a SQLite database tied to my uitableview. There are about 2000 rows, so I retrieve 25 rows at first. Once I scroll to the end of the list, I want it to automatically retrieve 25 more rows.
Here's the method I'm using now. Ignore what would happen if I load all 2000 rows, I'll worry about that later. In tableView:numberOfRowsInSection:
I return one more than the currently loaded rows. Then in tableView:cellForRowAtIndexPath:
I load more rows if it loads the last row.
Typically I can get down a few pages, but I eventually get an exception:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (10) beyond bounds (10)'
. The index is always 8-10. I don't know what array it is, but both my rows
and indices
array have well more than 10 items.
tableView:cellForRowAtIndexPath:
if(indexPath.row >= [rows count]) {
[cell.textLabel setText:@"Loading..."];
if(!updating && indexPath.row == [rows count]) {
updating = YES;
[[self tableView] beginUpdates];
NSMutableArray *indices = [NSMutableArray array];
// stmt = ...
while(sqlite3_step(stmt) == SQLITE_ROW) {
[rows addObject:@"..."];
[indices addObject:[NSIndexPath indexPathForRow:[rows count]-1 inSection:0]];
}
[[self tableView] insertRowsAtIndexPaths:indices withRowAnimation:UITableViewRowAnimationNone];
[[self tableView] endUpdates];
updating = NO;
}
return cell;
}
Stack trace:
#0 0x01ce4004 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ ()
#1 0x9779df49 in objc_exception_throw ()
#2 0x01cc5c3b in +[NSException raise:format:arguments:] ()
#3 0x01cc5b9a in +[NSException raise:format:] ()
#4 0x00072cc9 in _NSArrayRaiseBoundException ()
#5 0x00010227 in -[NSCFArray objectAtIndex:] ()
#6 0x0047fe5b in -[_UITableViewUpdateSupport(Private) _setupAnimationsForExistingVisibleCells] ()
#7 0x0047f9e0 in -[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:newRowData:oldRowRange:newRowRange:context:] ()
#8 0x002eca2b in -[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:] ()
#9 0x002ec757 in -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] ()
#10 0x002def56 in -[UITableView endUpdates] ()