0

我有一个以 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;
}
4

2 回答 2

0

尝试[[self tableView] reloadData]代替[[self tableView] endUpdates]

于 2012-04-07T17:30:20.430 回答
0

当您更新表格视图时,您也必须更新数据源。在您的代码中没有显示数据源更新方法,所以我认为应该考虑到这一点。

于 2012-04-07T11:50:05.300 回答