J'ai une base de données SQLite liée à mon uitableview. Il y a environ 2000 lignes, donc je récupère 25 lignes au début. Une fois que je fais défiler jusqu'à la fin de la liste, je veux qu'il récupère automatiquement 25 lignes de plus.Étendre automatiquement UITableView - NSRangeException
Voici la méthode que j'utilise maintenant. Ignorer ce qui se passerait si je charge toutes les 2000 lignes, je m'inquiéterai de cela plus tard. Dans tableView:numberOfRowsInSection:
j'en renvoie un de plus que les lignes actuellement chargées. Puis dans tableView:cellForRowAtIndexPath:
je charge plus de lignes si elle charge la dernière ligne.
Typiquement, je peux descendre quelques pages, mais je finis par obtenir une exception: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (10) beyond bounds (10)'
. L'index est toujours 8-10. Je ne sais pas de quel tableau il s'agit, mais mes deux matrices rows
et indices
ont bien plus de 10 éléments.
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;
}
Trace de la pile:
#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]()
C'est une excellente solution! Merci beaucoup! – Markus