【问题标题】:Crash on insertRowsAtIndexPathsinsertRowsAtIndexPaths 崩溃
【发布时间】:2017-01-11 05:27:11
【问题描述】:

我有聊天应用程序,我从服务器获取历史记录并逐条获取消息,因此我获取一条消息并将其添加到数组并将行插入表中......这个过程非常快。

这里是代码:

-(void)msgRecevied:(NSMutableDictionary *)messageContent
{
        if([chatWithUser isEqualToString:[messageContent objectForKey:kReceiver_User]])
            return;

        NSString *m = [messageContent objectForKey:kMsg];
        [messageContent setObject:[m substituteEmoticons] forKey:kMsg];
        [messageContent setObject:[messageContent objectForKey:kTrnDate] forKey:kTrnDate];
        [messageContent setObject:[messageContent objectForKey:kSender_User] forKey:kSender_User];
        [messageContent setObject:[messageContent objectForKey:kReceiver_User] forKey:kReceiver_User];

        NSMutableDictionary *arrayTemp1 = [messageContent mutableCopy];

        dispatch_async(dispatch_get_main_queue(), ^{


            [app.Glb.arrayChat addObject:arrayTemp1];
            numberOfRows  = app.Glb.arrayChat.count;

            if([app.Glb.arrayChat count] > 0)
            {

                [self.tblChatting beginUpdates];
                NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[app.Glb.arrayChat count]-1 inSection:0];
                [self.tblChatting insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
                [self.tblChatting endUpdates];

                [self.tblChatting scrollToRowAtIndexPath:newIndexPath
                                        atScrollPosition:UITableViewScrollPositionBottom
                                                animated:YES];
                newIndexPath = nil;
            }
        });
}

但我对endUpdates 崩溃了:

2016-09-03 10:47:32.638 [313:50270] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x2379f98b 0x22f3ae17 0x236b4e1f 0x2804372b 0x27d43a19 0x27d5aaa3 0x27f1a741 0x27efe987 0x7e93b 0x7a7b7f 0x7a7b6b 0x7ac655 0x23761bdd 0x237600d7 0x236af2e9 0x236af0d5 0x24c9fac9 0x27d740b9 0x39424d 0x23357873)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

  • 首先检查您的应用程序崩溃的位置。 -[__NSArrayM insertObject:atIndex:]: object cannot be nil 建议您在 Mutable Array 中插入 nil。
  • 但我没有在课堂上使用 insertObject:

标签: ios objective-c uitableview xmpp openfire


【解决方案1】:

检查您是否在 messageContent 中获取数据,因为错误表明您正在将 nil 对象添加到数组中。如果您从服务器获取数据,那么您应该从服务器调用或网络调用的completion handler 调用msgRecevied 方法。第二件事你不需要插入行。只需在您的数组(用作数据源)中添加新对象,然后只需 relaod tableview 即可,cellforrowatindexpath 将管理所有内容。

【讨论】:

  • 是的 reloadData 工作,但它失败了一段时间,它没有将消息单元格添加到表中。所以想出这个解决方案。我检查的消息内容不是空的。
  • 不,我说我知道 reloadData 解决了我的问题,但是当用户发送消息说时间表没有向表中添加行时它会出错......所以我想出了这个将行插入表的解决方案而不是重新加载表
  • 那一定是你的数据源的问题。你应该管理cellForRowAtIndexpath,这取决于你的数组并根据你的数组返回单元格!!
  • 新对象添加到 arrayChat 并重新加载表.. 但它没有添加行。我得到 numberOfRow 增加但行未添加到表中。
  • 显示你的 cellforrow 方法和你的数组!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2021-05-14
相关资源
最近更新 更多