UITableView insertRowsAtIndexPaths抛出__NSArrayM insertObject:atIndex:'object not not nil'错误 [英] UITableView insertRowsAtIndexPaths throwing __NSArrayM insertObject:atIndex:'object cannot be nil' error

查看:178
本文介绍了UITableView insertRowsAtIndexPaths抛出__NSArrayM insertObject:atIndex:'object not not nil'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert an item to my table view dynamically. My app has a chat section, where it displays the old (loaded from server before initializing the view controller) messages in section 0, and displays just sent/received messages (where it is initially zero) in section 1. When the view is loaded, all the "old" messages are loaded and displayed, no problem is there. The problem starts when I try to insert rows. Here is what I am doing:


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

  • 然后我调用以下代码:

  • I am first updating my table view's data source by adding an extra item: [newMessages addObject:newMessage]; (newMessage is an instance of my custom message object, and newMessages is my data source). I verify that my data source now has 1 item (which was 0 before adding).
  • I then call the following code:

[self.chatTableView beginUpdates];
[self.chatTableView insertRowsAtIndexPaths:@[[NSIndexPath 
    indexPathForRow:newMessages.count - 1 inSection:1]] 
    withRowAnimation:UITableViewRowAnimationBottom];
[self.chatTableView endUpdates];


我的应用程序在 endUpdates 方法,给我这个错误: ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'*** - [__ NSArrayM insertObject:atIndex:]:对象不能为零'

My app crashes at endUpdates method, giving me this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

我已立即检查 newMessage 是否 nil 是否 nil (并重新检查我的数据源)所以数据源不是问题。我认为 indexPathForRow:newMessages.count - 1 可能是问题并尝试了不同的值(count-2,count,count + 1)以防万一我丢失一些东西。在这些情况下,我收到另一个错误:'NSInternalInconsistencyException',原因:'尝试将第1行插入第1部分,但更新后第1部分只有1行'。错误说明了一切,所以问题不在于 indexPathForRow:newMessages.count - 1

I've immediately checked if newMessage is nil or not, it's not nil (and re-checked my data source) so data source is not the problem. I've thought that indexPathForRow:newMessages.count - 1 could be the problem and tried different values (count - 2, count, count + 1) just in case I was missing something. In those cases, I'm getting another error: 'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 1, but there are only 1 rows in section 1 after the update'. The error says it all, so the problem is not something with indexPathForRow:newMessages.count - 1 either.

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

I've added breakpoints into the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method to see when it's exactly called and what it returns. It seems that the method is not called at all, the breakpoint is not hitting (it DOES hit when view is first loaded and loads the initial data correctly, and I'm not setting data source anywhere, so delegates/data sources are also connected correctly). Immediately I've checked other methods:

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

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

这些方法返回正确的值。我在 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 中放置一个断点来查看它何时被调用。显然 [self.chatTableView endUpdates]; 方法中调用(从线程/队列的调用堆栈中看到),但随后<$> c $ c> [self.chatTableView endUpdates]; 立即抛出错误,甚至没有输入 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法。我见过例子(以及其他一些例子),例如:

Those methods return correct values. I've put a breakpoint in -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView to see when it's called. It apparently is called inside the [self.chatTableView endUpdates]; method (as seen from the thread/queue's call stack), but then [self.chatTableView endUpdates]; immediately throws the error without even entering -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method. I've seen examples (and a few others) such as:

  • how to properly use insertRowsAtIndexPaths?
  • Error creating row in tableview
  • Add cell to bottom of UITableView in iOS
  • Tableview crashes when moving more than half the cells to another section

我在那里读到了答案,但没有一个能帮助我。我做错了什么?

I've read the answers there, but none of them helped me. What am I doing wrong?

推荐答案

你有 tableView:estimatedHeightForRowAtIndexPath:实施?我注意到在我自己的一个应用程序中使用 NSFetchedResultsController 的类似问题,当 tableView:endUpdates时,应用程序将崩溃并显示相同的错误消息被调用。评论 tableView:estimatedHeightForRowAtIndexPath:为我修复它。

Do you have tableView:estimatedHeightForRowAtIndexPath: implemented? I've noticed a similar issue inside one of my own applications that used an NSFetchedResultsController where the application would crash with the same error message when tableView:endUpdates was called. Commenting out tableView:estimatedHeightForRowAtIndexPath: fixed it for me.

这篇关于UITableView insertRowsAtIndexPaths抛出__NSArrayM insertObject:atIndex:'object not not nil'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆