Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically expand UITableView - NSRangeException
    text
    copied!<p>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. </p> <p>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 <code>tableView:numberOfRowsInSection:</code> I return one more than the currently loaded rows. Then in <code>tableView:cellForRowAtIndexPath:</code> I load more rows if it loads the last row.</p> <p>Typically I can get down a few pages, but I eventually get an exception: <code>*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (10) beyond bounds (10)'</code>. The index is always 8-10. I don't know what array it is, but both my <code>rows</code> and <code>indices</code> array have well more than 10 items.</p> <p><code>tableView:cellForRowAtIndexPath:</code></p> <pre><code>if(indexPath.row &gt;= [rows count]) { [cell.textLabel setText:@"Loading..."]; if(!updating &amp;&amp; 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; } </code></pre> <p>Stack trace:</p> <pre><code>#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] () </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload