![](/img/trans.png)
[英]UITableViewAutomaticDimension for Cell or Header View height causes crash on UITableView insertRowsAtIndexPaths()
[英]Crash on insertRowsAtIndexPaths
我有聊天应用程序,我从服务器获取历史记录,并且得到一条一条消息,因此我获取一条消息并将其添加到数组中,然后将行插入表中..这个过程非常快。
这是代码:
-(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
that:
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
检查您是否在messageContent
中获取数据,因为错误表明您正在向数组添加nil对象。 如果要从服务器获取数据,则应从服务器调用或网络调用的completion handler
中调用msgRecevied
方法。 第二件事,您不需要插入行。 只是你的数组中添加新的对象(被用作数据源),只是relaod
tableview中,如此后cellforrowatindexpath
将管理所有的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.