insertRowsAtIndexPaths

时间:2016-09-03 05:24:07

标签: ios objective-c uitableview xmpp openfire

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

这是代码:

-(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

1 个答案:

答案 0 :(得分:0)

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