【发布时间】:2009-09-21 00:09:55
【问题描述】:
我有一个绑定到我的 uitableview 的 SQLite 数据库。大约有 2000 行,所以我首先检索 25 行。一旦我滚动到列表的末尾,我希望它自动检索 25 多行。
这是我现在使用的方法。忽略如果我加载所有 2000 行会发生什么,我稍后会担心。在tableView:numberOfRowsInSection: 中,我返回的比当前加载的行多一个。然后在tableView:cellForRowAtIndexPath: 中,如果加载最后一行,我会加载更多行。
通常我可以写几页,但我最终得到一个例外:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (10) beyond bounds (10)'。该指数始终为 8-10。我不知道它是什么数组,但我的 rows 和 indices 数组都有超过 10 项。
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;
}
堆栈跟踪:
#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] ()
【问题讨论】:
标签: ios objective-c iphone uitableview cocoa-touch