我有一个以 C++ 对象为数据源的 UITableView,因此没有任何类型的 NSArray。每当我想在现有列表的末尾插入一行时,我就会崩溃
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]
它在 [tableView endUpdates] 上崩溃。但是,我的实现中没有任何 NSArray 或任何调用 objectAtIndex 的代码。我不知道为什么我会崩溃。
我只有一个部分,并且总是将新行添加到第 0 部分的末尾。返回的行数是正确的,因此完全同步。
在我看到的日志中, [tableView endUpdates] 调用 numberOfRowsInSection 正确返回了前一个计数加一的计数。但是,它再也不会调用 cellForRowAtIndexPath 并且无法通过 [tableView endUpdates];
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"...");
........
NSArray *insertIndexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:oldCount + i inSection:[indexPath section]], nil];
NSLog(@"Insert: %d %d %d", i, (int) oldCount, (int) newCount);
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int i = _reader->allRead() ? _reader->getWindowCount() : _reader->getWindowCount() + 30;
return i;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
// return __results__ ? 1 : 0;
}