我的tableView在重新加载行超过600时冻结。有没有任何解决方案。我在后台线程中获取数据并尝试在主线程中重新加载我的表但仍然是问题相同。
这是我在cellForRow中的代码..
if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) {
identifier = @"onlyRightChat";
}else{
identifier = @"onlyLeftChat";
}
UITableViewCell *cell ;
cell= [tableView dequeueReusableCellWithIdentifier:identifier ];
NSString *userName;
if ([chatDict valueForKey:@"sender_last_name"]) {
userName = [NSString stringWithFormat:@"%@ %@",[chatDict valueForKey:@"sender_first_name"],[chatDict valueForKey:@"sender_last_name"]];
}else{
userName = [NSString stringWithFormat:@"%@",[chatDict valueForKey:@"sender_first_name"]];
}
NSString *str ;
if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) {
str = [NSString stringWithFormat:@"%@:\n%@",userName,[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:@"text"]]];
}else{
str = [NSString stringWithFormat:@"You:\n%@",[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:@"text"]]];
}
CGRect labelSize =[self getRectForString:str];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
TTTAttributedLabel *lbl = (TTTAttributedLabel *)[cell.contentView viewWithTag:5111];
UIView *lblBgView = (UIView *)[cell.contentView viewWithTag:5113];
UIView *bgView = (UIView *)[cell.contentView viewWithTag:5115];
UILabel *timeLbl = (UILabel *)[cell.contentView viewWithTag:5114];
UIImageView *userImgView = (UIImageView *)[cell.contentView viewWithTag:5116];
UILabel *onlineIcon = (UILabel *)[cell.contentView viewWithTag:onlineStatusTag];
onlineIcon.layer.cornerRadius = onlineIcon.frame.size.height/2.0;
onlineIcon.clipsToBounds = YES;
lbl.backgroundColor = [UIColor clearColor];
lbl.enabledTextCheckingTypes = NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress;
lbl.delegate = self;
if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) {
lbl.linkAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:91/255.0f green:135/255.0f blue:198/255.0f alpha:1.0] ,NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};//[UIColor colorWithRed:119/255.0f green:195/255.0f blue:66/255.0f alpha:1.0]
}else{
lbl.linkAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:194/255.0f green:228/255.0f blue:255/255.0f alpha:1.0] ,NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};//[UIColor colorWithRed:119/255.0f green:195/255.0f blue:66/255.0f alpha:1.0]
}
if ([currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) {
onlineIcon.hidden = YES;
}else{
onlineIcon.hidden = NO;
NSString *status = [chatDict valueForKey:@"status"];
if(status){
if([status isEqualToString:@"online"]){
onlineIcon.backgroundColor = [UIColor colorWithRed:81/255.0 green:217/255.0 blue:93/255.0 alpha:1.0];
}else if([status isEqualToString:@"offline"]){
onlineIcon.backgroundColor = [UIColor yellowColor];
}else{
onlineIcon.backgroundColor = [UIColor colorWithRed:214/255.0 green:103/255.0 blue:101/255.0 alpha:1.0];
}
}else{
onlineIcon.backgroundColor = [UIColor yellowColor];
}
}
userImgView.layer.cornerRadius = userImgView.frame.size.height/2;
userImgView.clipsToBounds = YES;
[userImgView sd_setImageWithURL:[NSURL URLWithString:[chatDict valueForKey:@"sender_picture"]]placeholderImage:[UIImage imageNamed:@"default_avatar"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userImageViewTap:)];
tapGesture.numberOfTapsRequired = 1;
userImgView.userInteractionEnabled = YES;
[userImgView addGestureRecognizer:tapGesture];
// NSLog(@"cell.frame.size.width %f",cell.frame.size.width);
float width = cell.frame.size.width;
float height = cell.frame.size.height;
if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]])
{
bgView.frame = CGRectMake(width- labelSize.size.width-31-20-35,0,labelSize.size.width+31+20,cell.frame.size.height);
lblBgView.frame = CGRectMake(0,10,bgView.frame.size.width-15,labelSize.size.height+10);
lbl.frame = CGRectMake(8, 0, lblBgView.frame.size.width-16, labelSize.size.height+10);
timeLbl.frame = CGRectMake(width-170,height-25, 120,fontSize);
timeLbl.textAlignment = NSTextAlignmentRight;
}else{
bgView.frame = CGRectMake(35,0,labelSize.size.width+31+20,cell.frame.size.height);
lblBgView.frame = CGRectMake(15,10,bgView.frame.size.width-15,labelSize.size.height+10);
lbl.frame = CGRectMake(8, 0, lblBgView.frame.size.width-16, labelSize.size.height+10);
timeLbl.frame = CGRectMake(55,height-25, 120,fontSize);
timeLbl.textAlignment = NSTextAlignmentLeft;
}
NSDate* dateComment = [[Utils sharedObject] StringToDate:[chatDict valueForKey:@"created_at"] withFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
timeLbl.text = [[Utils sharedObject] relativeTimestamp:dateComment];
timeLbl.font = [UIFont fontWithName:@"Lato-Regular" size:fontSize-3.0];
lblBgView.layer.cornerRadius = 8.0f;
lblBgView.clipsToBounds = YES;
lbl.layer.cornerRadius = 8.0f;
lbl.font = [UIFont fontWithName:@"Lato-Regular" size:fontSize];
lbl.numberOfLines = 0;
lbl.clipsToBounds = YES;
if (str!=(id)[NSNull null])
{
if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) {
lbl.text = str;
}else{
NSDictionary *attrDict = @{ NSFontAttributeName : [UIFont fontWithName:@"Lato-Regular" size:fontSize], NSForegroundColorAttributeName : [UIColor colorWithRed:146/255.0 green:183/255.0 blue:201/255.0 alpha:1.0]};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"You:\n" attributes: attrDict];
NSDictionary *attrDict1 = @{ NSFontAttributeName : [UIFont fontWithName:@"Lato-Regular" size:fontSize]};
NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:@"text"]] attributes: attrDict1];
[attrString appendAttributedString:attrString1];
lbl.text = str;
lbl.textColor = [UIColor whiteColor];
}
}else{
lbl.text= @"";
}
return cell;
}
并从方法重新加载调用..
-(void)getChatTextFromLocalDB{
// [self showLoadingView];
NSString *databaseKey = [NSString stringWithFormat:@"%@-%@",[self.m_dictInfo valueForKey:@"rid"],[[NSUserDefaults standardUserDefaults] objectForKey:@"rid"]];
NSString *localDBPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/GroupChat.plist"];
NSMutableDictionary *root = [[[NSMutableDictionary alloc] initWithContentsOfFile:localDBPath] mutableCopy];
NSMutableArray *chatDictAry = [root objectForKey:databaseKey];
NSString *localDBPath1=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/GeoConnectChat.plist"];
NSMutableDictionary *root1 = [[[NSMutableDictionary alloc] initWithContentsOfFile:localDBPath1] mutableCopy];
NSMutableArray *temp = [root1 objectForKey:@"chatDeleted"];
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:YES];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
NSArray *sortedArray = [chatDictAry sortedArrayUsingDescriptors:sorters];
NSString *lastMsgRid;
[chatDictAry removeAllObjects];
[chatDictAry addObjectsFromArray:sortedArray];
if(chatDictAry.count||[temp containsObject:databaseKey ]){
[self.chatArray removeAllObjects];
for (NSDictionary *dict in chatDictAry) {
if(!([g_Delegate.grpBlockByMeList containsObject:[dict valueForKey:@"sender"]]||[g_Delegate.grpBlockMeList containsObject:[dict valueForKey:@"sender"]])){
[self.chatArray addObject:dict];
if([dict valueForKey:@"rid"])
lastMsgRid = [dict valueForKey:@"rid"] ;
[self.usersArray addObject:[dict valueForKey:@"sender"]];
}
}
[root writeToFile:localDBPath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self hideLoadingView];
});
if (self.chatArray.count) {
if(tablePositionTop){
CGSize beforeContentSize = self.chatTableView.contentSize;
[self.chatTableView reloadData];
CGSize afterContentSize = self.chatTableView.contentSize;
CGPoint afterContentOffset = self.chatTableView.contentOffset;
CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + afterContentSize.height - beforeContentSize.height);
self.chatTableView.contentOffset = newContentOffset;
}
else{
[self.chatTableView reloadData];
int64_t delayInSeconds = 0.2f;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if(actualSelf){
[self.chatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.chatArray count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
});
}
[self getUsersOnlineStatus];
if (isFirstTime) {
isFirstTime = NO;
[self getRemainingListFromServerWithLastMsgId:lastMsgRid];
}else{
// [self hideLoadingView];
}
}else{
[self hideLoadingView];
}
}
else
{
if (isFirstTime) {
isFirstTime = NO;
[self showLoadingView];
[self getAllMessages];
}
}
}
答案 0 :(得分:0)
我建议你观看WWDC 2012的Session 211,构建并发用户界面并在那里应用这些概念。这包含其内容被独立查询和呈现的单元格。
基本概念如下:
1.在tableView:cellForRowAtIndexPath中,实例化一个单元格
2.在相同的方法中,创建用于检索数据以填充单元格的操作并将其存储到字典中。对单元格的引用传递给操作。该操作有一个完成处理程序,用于填充单元格并从字典中删除操作
3.在从方法返回单元格之前,将操作添加到操作队列中
4.在tableView:didEndDisplayingCell:forRowAtIndexPath中,已取消已移出屏幕的单元格的操作将从字典中删除。