iOS tableViewCell 在cell赋值、网络加载照片位置偏移大小错乱,做一个类似qq列表的tableview 更新3
更新3: 问题 加载慢!
(一时间给的处理负载过大,要分散)在下载图片,判断状态后 对每个cell对图片灰置图片处理保存,影响了主线程的操作
:上拉加载时,无法上下滑动tableview 无法点击cell
: 去除对每一个图片对灰置保存,只保存彩色,在需要灰色时在灰置,
需求: 类似QQ列表的头像加载刷新,判断在线离线状态改变头像,以及彩色头像灰色处理,下载图片+获取在线状态需要连网--再改变头像
问题:由于cell的复用以及下拉刷新数据每次加载10条数据,会出现头像赋值不正确,位置偏移大小不同的变化
原因:由于cell的重复调用,加载数据方法已经赋值方法也在重复的调用,所以头像加载 在线状态判断好后,网络延迟,
(个人开始yy:启动时的cell和赋值结束的cell可能不是同一个)
修改:当cell开始调用的时候,给当前的cell赋tag值,加载结束判断是不是自己的tag:不是了跳过是了赋值。反正可以解决问题
避免头像的重复加载,更换:由于每次进来都要重新加载一组新的头像,并没有做本地化处理;
此处理会影响主线程操作:在控制器中添加一个可变的字典dict ; dict= {@"0":image,@"1":image,@"2":image,@"3":@"default",@"lasttime":@"0/1/2"}
0是离线的image;1在线;2忙碌;3有值(使用默认头像,不存入字典);lasttime 记录上一次的状态,刷新时首先显示上一次的状态值,刷新状态后:对比上一次,
如果和上一次不一样了在更换;
//头像灰化处理
//置灰 +(UIImage*)createGrayCopy:(UIImage*)source{ int width = source.size.width; int height = source.size.height; CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceGray(); CGContextRef context =CGBitmapContextCreate(nil, width, height, 8,// bits per component 0, colorSpace, kCGBitmapByteOrderDefault); CGColorSpaceRelease(colorSpace); if(context ==NULL) { return nil; } CGContextDrawImage(context, CGRectMake(0,0, width, height), source.CGImage); UIImage*grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)]; CGContextRelease(context); return grayImage; }
//下载图片
//根据uid 取出相应的字典
NSMutableDictionary * dicc = arrayAllImage[model.uid]; //判断是否保存过图片 if (dicc[@"1"]) { //之前保存过值 cell.imageview.image = dicc[@"1"]; if ([dicc[lasttime] isEqualToString:@"2"]) { cell.busyOr.hidden = NO; }else{ cell.busyOr.hidden = YES; } }else{ //没有的话 下载 保存,设置在线状态 0离线灰色头像 1在线2忙碌彩色头像 if ([model.status isEqualToString:@"1"]) { [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:[UIImage imageNamed:HeadPortraitDoctor]]; dicc =[NSMutableDictionary dictionary]; [dicc setObject:cell.imageview.image forKey:@"1"]; [dicc setObject:@"1" forKey:lasttime]; [arrayAllImage setObject:dicc forKey:model.uid]; cell.busyOr.hidden = YES;//隐藏 忙碌状态 } else if([model.status isEqualToString:@"0"]){ [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:nil]; //不在线 cell.busyOr.hidden = YES; dicc =[NSMutableDictionary dictionary]; if (cell.imageview.image) { cell.imageview.image = [UIImage createGrayCopy:cell.imageview.image]; }else{ cell.imageview.image = graimage; [dicc setObject:graimage forKey:@"1"]; } [dicc setObject:cell.imageview.image forKey:@"1"]; [dicc setObject:@"0" forKey:lasttime]; [arrayAllImage setObject:dicc forKey:model.uid]; } else if([model.status isEqualToString:@"2"]){ //忙碌 [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:[UIImage imageNamed:HeadPortraitDoctor]]; dicc =[NSMutableDictionary dictionary]; [dicc setObject:cell.imageview.image forKey:@"1"]; [dicc setObject:@"2" forKey:lasttime]; [arrayAllImage setObject:dicc forKey:model.uid]; cell.busyOr.hidden = NO; } } if(!ScrollerYesNo){ //给图片标记 cell.imageview.tag = indexPath.row+200; [[GetUrlSession shareUrlconnection]connetion:[NSString stringWithFormat:@"%@?uid=%@&port=%@",searchOnlineStatusGet,model.uid,model.port] Haget:^(NSDictionary *data, NSError *error, NSHTTPURLResponse *response) { NSString* onlineStatue = [NSString stringWithFormat:@"%@",data[@"status"]]; dispatch_async(dispatch_get_main_queue(), ^{ NSMutableDictionary * diccc = [NSMutableDictionary dictionary]; [diccc setDictionary: arrayAllImage[model.uid]]; //判断当前的状态。 以及是否上一次的状态值 if ([onlineStatue isEqualToString:@"1"] && ![onlineStatue isEqualToString:diccc[lasttime]]) { UIImageView *lastImageView = [self.view viewWithTag:indexPath.row+200]; if (![lastImageView isEqual:cell.imageview]) { return ; } //在线 [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:[UIImage imageNamed:HeadPortraitDoctor]]; [dicc setObject:cell.imageview.image forKey:@"1"]; [dicc setObject:@"1" forKey:lasttime]; [arrayAllImage setObject:dicc forKey:model.uid]; cell.busyOr.hidden = YES; }else if([onlineStatue isEqualToString:@"0"] && ![onlineStatue isEqualToString:diccc[lasttime]]){ //不在线 cell.busyOr.hidden = YES; UIImageView *lastImageView = [self.view viewWithTag:indexPath.row+200]; if (![lastImageView isEqual:cell.imageview]) { return ; } [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:nil]; if (cell.imageView.image) { cell.imageview.image = [UIImage createGrayCopy:cell.imageview.image]; }else{ cell.imageview.image = graimage; } [dicc setObject:cell.imageview.image forKey:@"1"]; [dicc setObject:@"0" forKey:lasttime]; [arrayAllImage setObject:dicc forKey:model.uid]; } else if([onlineStatue isEqualToString:@"2"] && ![onlineStatue isEqualToString:diccc[lasttime]] ){ //忙碌 UIImageView *lastImageView = [self.view viewWithTag:indexPath.row+200]; if (![lastImageView isEqual:cell.imageview]) { return ; } //在线 [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:[UIImage imageNamed:HeadPortraitDoctor]]; [dicc setObject:cell.imageview.image forKey:@"1"]; [dicc setObject:@"2" forKey:lasttime]; [arrayAllImage setObject:dicc forKey:model.uid]; cell.busyOr.hidden = NO; } }); }]; }