UITableView insertRowsAtIndexPaths抛出__NSArrayM insertObject:atIndex:'object not not nil'错误

Can*_*ğlu 14 uitableview ios ios7

我试图动态地将一个项目插入到我的表视图中.我的应用程序有一个聊天部分,它在第0部分显示旧的(在初始化视图控制器之前从服务器加载)消息,并在第1部分显示刚发送/接收的消息(最初为零).当视图加载时,所有"旧"消息都被加载和显示,没有问题.当我尝试插入行时,问题就开始了.这是我在做的事情:

  • 我首先通过添加一个额外的项来更新我的表视图的数据源:( [newMessages addObject:newMessage];newMessage是我的自定义消息对象的实例,newMessages是我的数据源).我验证我的数据源现在有1个项目(在添加之前为0).
  • 然后我调用以下代码:

    [self.chatTableView beginUpdates];
    [self.chatTableView insertRowsAtIndexPaths:@[[NSIndexPath 
        indexPathForRow:newMessages.count - 1 inSection:1]] 
        withRowAnimation:UITableViewRowAnimationBottom];
    [self.chatTableView endUpdates];
    
    Run Code Online (Sandbox Code Playgroud)

我的应用程序在endUpdates方法崩溃,给我这个错误:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

我立即检查,如果newMessagenil或不是,它是不会 nil(并重新检查了我的数据源),这样的数据源是没有问题的.我认为这indexPathForRow:newMessages.count - 1可能是问题,并尝试了不同的值(计数-2,计数,计数+ 1),以防我错过了什么.在那些情况下,我收到了另一个错误:'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 1, but there are only 1 rows in section 1 after the update'.错误说明了一切,所以问题也不indexPathForRow:newMessages.count - 1是.

我在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中添加了断点,以查看它何时被完全调用以及它返回的内容.似乎根本没有调用该方法,断点没有命中(它在首次加载视图时被点击并正确加载初始数据,而我没有在任何地方设置数据源,因此委托/数据源也连接正确).我立刻检查了其他方法:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"Eski mesajlar";
    }else{
        return @"Yeni mesajlar";
    }
}
Run Code Online (Sandbox Code Playgroud)

这些方法返回正确的值.我已经设置了一个断点-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView来查看它何时被调用.它显然[self.chatTableView endUpdates];方法内部调用的(从线程/队列的调用堆栈中看到),但是[self.chatTableView endUpdates];在没有输入-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法的情况下立即抛出错误.我见过例子(以及其他一些例子),例如:

我已经在那里阅读了答案,但他们都没有帮助我.我究竟做错了什么?

Kam*_*ros 30

tableView:estimatedHeightForRowAtIndexPath:实施了吗?我已经注意到我自己的一个应用程序中存在类似的问题,该问题使用了一个NSFetchedResultsController应用程序tableView:endUpdates在调用时会出现相同错误消息的情况.评论出来tableView:estimatedHeightForRowAtIndexPath:为我修好了.

  • 我不认为这个"bug"是完全解决的,因为我遇到了同样的崩溃/异常问题并且注释掉了tableView:estimatedHeightForRowAtIndexPath:让它消失(但是让tableView滚动生涩并且无法使用.) (3认同)
  • 当你删除`tableView:estimatedHeightForRowAtIndexPath:`方法(或停止使用`estimatedRowHeight`属性)时,只需确保你完全理解你在做什么.您现在强制表视图为表格视图中的每个**行调用`tableView:heightForRowAtIndexPath:`,然后在每次调用`reloadData`或类似时显示任何内容*和* - 主要性能影响!因此,删除行高估计的使用并不是一个真正的修复,只是一个似乎是一个苹果bug的解决方法.(@CanPoyrazoğlu你有你提交的bug的雷达#) (2认同)

小智 11

关于删除/注释tableView:estimatedHeightForRowAtIndexPath:,这不是我如何解决它,因为我没有在我的代表上使用该方法.

相反,我实际上已经设置tableView.estimatedSectionHeaderHeight = 10.;了我的viewDidLoad.简单地通过简单地注释掉该行,异常不再发生,然后这就像我期望的那样工作.

但这太糟糕了,因为我不能说为什么评论这个(或estimatedHeightForRowAtIndexPath:方法)修复它.