tableView最终导致超过16个项目崩溃

时间:2013-12-15 14:42:11

标签: ios iphone objective-c uitableview crash

这非常令人困惑。

我有一个UITableView,它会更新并正常工作,直到它获得超过16个项目,然后在调用endUpdates后尝试insertRowsAtIndexPaths时崩溃。

添加的NSIndexPath全部有效。 -numberOfRowsInSection返回正确的数字。它不会抛出与数据集相关的错误,而是与

崩溃
  

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'*** - [__ NSArrayM insertObject:atIndex:]:object不能为nil'

endUpdates上调用tableView

数据源就在那里,NSIndexPath很好。代码在0到16行之间正常工作,但是当我添加第17行时它会崩溃。另外如果我用22个项目启动它可以正常工作,当我添加第23个它崩溃时...如果我调用重载数据而不是执行更新和插入过程它工作正常,所以它与数据本身无关,并且它应该与我插入行的方式无关,因为它通过16 ...

工作

我完全感到困惑。这是我的更新方法。它始终在主线程上被调用。

- (void)updateConversation:(NSNotification*)notification
{
    NSDictionary *updateInfo = [notification userInfo];
    //NSLog(@"Got update %@", updateInfo);

    if ([[updateInfo objectForKey:@"success"] integerValue] == YES) {
        [self updateConversationUI];

        int addedStatementCount = [[updateInfo objectForKey:@"addedStatementCount"] intValue];

        if (addedStatementCount > 0) {
            //[self.tableView reloadData];
            [self.tableView beginUpdates];
            int previousStatmentCount = [[updateInfo objectForKey:@"previousStatmentCount"] intValue];

            NSLog(@"owner %i, Was %i, now %i, change of %i", self.owner, previousStatmentCount, (int)self.conversation.statements.count, addedStatementCount);

            NSMutableArray *rowPaths = [[NSMutableArray alloc] init];

            for (int i = previousStatmentCount; i < previousStatmentCount + addedStatementCount; i++) {
                NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
                [rowPaths addObject:path];
            }

            [self.tableView insertRowsAtIndexPaths:rowPaths withRowAnimation:UITableViewRowAnimationBottom];        
            [self.tableView endUpdates];
            [self.tableView scrollToRowAtIndexPath:[rowPaths lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
}

超过[self.tableView endUpdates]的其余崩溃是UITableView

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010189b795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001015fe991 objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101852564 -[__NSArrayM insertObject:atIndex:] + 820
    3   UIKit                               0x0000000100317900 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke691 + 173
    4   UIKit                               0x00000001002b5daf +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 460
    5   UIKit                               0x00000001002b6004 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57
    6   UIKit                               0x00000001003174cb -[UITableView _updateWithItems:updateSupport:] + 2632
    7   UIKit                               0x0000000100312b18 -[UITableView _endCellAnimationsWithContext:] + 11615
    8   Dev App                         0x0000000100006036 -[ConversationViewController updateConversation:] + 998
    9   CoreFoundation                      0x00000001018f121c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    10  CoreFoundation                      0x000000010185370d _CFXNotificationPost + 2381
    11  Dev App                         0x00000001000055ac -[ConversationManager postNotification:] + 92
    12  Foundation                          0x0000000101204557 __NSThreadPerformPerform + 227
    13  CoreFoundation                      0x000000010182aec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010182a792 __CFRunLoopDoSources0 + 242
    15  CoreFoundation                      0x000000010184661f __CFRunLoopRun + 767
    16  CoreFoundation                      0x0000000101845f33 CFRunLoopRunSpecific + 467
    17  GraphicsServices                    0x00000001039a23a0 GSEventRunModal + 161
    18  UIKit                               0x0000000100261043 UIApplicationMain + 1010
    19  Dev App                         0x0000000100003613 main + 115
    20  libdyld.dylib                       0x0000000101f2a5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这似乎是操作系统中的一个错误,堆栈会表明它与动画块有关,但无论我将动画设置为什么,它都会发生,包括没有。

并重新陈述。这通过16个项目,然后插入过去的项目导致崩溃。在初始设置时也会调用它来加载数据,并且它将在那里加载任意数量的项目,包括16以上。但是当再次调用时,纯粹作为从16或更高的更新到更高的更新它将崩溃。

我还没有遇到过表格视图这个奇怪的问题......

在iOS 7下的设备和模拟器上,最新的Xcode供参考。

4 个答案:

答案 0 :(得分:20)

似乎问题是由我调用scrollToRowAtIndexPath引起的(即使它在它到达之前崩溃了......)结合实现tableView:estimatedHeightForRowAtIndexPath:通过删除行高估计崩溃消失了......似乎仍然存在就像我的桌子动画系统中的一个错误。值得庆幸的是,我不需要估计行高,我已经忘记了我已经实现了它(试图通过iOS 7玩得很好再咬我)。

答案 1 :(得分:0)

递增语句计数时放置策略断点,完成根据需要多次添加行的循环,然后找到声明它导致崩溃,此时需要查看您的对象查找nil值作为您尝试插入的错误数组中的nil对象(或不存在)。

我个人建议你完全完成这个循环,同时保留路径和先前语句计数的exe。

            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];

答案 2 :(得分:0)

在我的情况下,tableView:heightForHeaderInSection:方法已经丢失, 没有实现tableView:estimatedHeightForRowAtIndexPath方法,所以如果你没有使用tableView,这也可以解决问题:estimatedHeightForRowAtIndexPath:

答案 3 :(得分:-2)

您的问题是您没有更新tableView正在使用的dataSource。 如果插入一行,则需要将相应的对象插入tableView正在使用的数组中。

古德勒克!