UITableView Auto Extension - NSRangeException

I have a SQLite database attached to my uitableview. There are about 2,000 lines, so I extract 25 lines first. When I go to the end of the list, I want it to automatically extract another 25 lines.

Here is the method I'm using now. Ignore what happens if I load all 2000 lines, I will worry about this later. In tableView:numberOfRowsInSection:I return one more than the currently loaded lines. Then in tableView:cellForRowAtIndexPath:I load more lines if it loads the last line.

Usually, I can go down to a few pages, but in the end 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 do not know which array it is, but both are arrays rowsand indiceshave more than 10 elements.

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] ()    
+3
2

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

tableView:numberOfRowsInSection: ( SQLite COUNT , ) ( - , ), " ".

, , , , , , .

- :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] >= [cache count]) {
        // Load additional items into the cache
    }

    // Do the regular cell setup stuff here; you can assume the data you need is available
    return cell;
}
+4

, , , .

tableview - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath , , , , - .

, , , . , , . , , , , , .

0

Source: https://habr.com/ru/post/1718136/


All Articles