我创建了一个聊天应用程序。我收到了这种方法的消息:
-(void)msgRecevied:(NSMutableDictionary *)messageContent
{
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];
[arrayChat addObject:messageContent];
NSLog(@"%@",arrayChat);
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tblChat reloadData];
});
if(arrayChat.count > 1){
NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:arrayChat.count-1
inSection:0];
[self.tblChat scrollToRowAtIndexPath:topIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
}
当我收到消息并重新加载表时,调用的numberOfRowsInSection
消息被添加到数组中,并且我还在numberOfRowsInSection
中打印了数组计数,它也增加但不会被称为cellForRowAtIndexPath
所以无法显示我的信息。
我也尝试重新加载没有主线程但没有工作的表。
那里有什么问题?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%lu",[arrayChat count]);
return [arrayChat count];
}
修改
抱歉,但是这个问题是在应用程序来自后台后发生的。
我prtinted table:当app运行时:
2016-08-22 11:21:02.465 Diamonds[16679:6710554]
<UITableView: 0x12e0a6200; frame = (0 55; 250 332); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x132106f50>; animations = { position=<CASpringAnimation: 0x1320e3b40>; bounds.origin=<CASpringAnimation: 0x132173610>; bounds.size=<CASpringAnimation: 0x132173700>; bounds.origin-2=<CASpringAnimation: 0x13215c1b0>; bounds.size-2=<CASpringAnimation: 0x13215c2a0>;
};
layer = <CALayer: 0x132105a20>; contentOffset: {0, 3609}; contentSize: {250, 3941.197265625}>
来自背景后:
2016-08-22 11:19:51.910 Diamonds[16676:6709818]
<UITableView: 0x1388e2e00; frame = (0 55; 250 0);clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x13ca33040>; layer = <CALayer: 0x13c9e2bf0>; contentOffset: {0, 3125}; contentSize: {250, 3855.68359375}>
修改
我还发现了一个错误,当我添加对象添加到arrayChat时,但是当我重新加载数据第二次时,应用程序来自后台后添加的最后一个对象丢失了。
答案 0 :(得分:0)
您希望在后台线程中完成耗时的数据加载,然后在主线程上准备好数据时更新UI / Cells。
dispatch_queue_t concurrentQueue = dispatch_queue_create("Queue", NULL);
dispatch_async(concurrentQueue, ^{
[self loadData];
});
-(void)loadData
{
// Expensive operations add/remove data to NSArray or NSDictionary
.
.
// Operation done - now let's update our table cells on the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tblChat reloadData];
});
}