由于可重用性问题,tableview 单元格上的图像重复
Posted
技术标签:
【中文标题】由于可重用性问题,tableview 单元格上的图像重复【英文标题】:image on tableview cell repeating due to reusability issue 【发布时间】:2016-03-28 09:33:45 【问题描述】:我有一个表格视图单元格,有标签和图像,在滚动图像和标签时发生变化,这是由于单元格出列功能。我试图通过将 imageview 设置为 nil 来解决它,但它没有工作。 重复的问题在于 PinImageView,它从一组图像中获取图像,而不是从 URL 中获取图像。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier=@"cellIdentifier";
TripInfoListCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
cell=(TripInfoListCell *)[[[NSBundle mainBundle] loadNibNamed:@"TripInfoListCell" owner:self options:nil] firstObject];
cell.PinImageView.image = nil;
NSString *direction=[dict123 valueForKey:@"direction" ] ;
NSString *drive_status=[dict123 valueForKey:@"driver_or_rider"];
if((indexPath.row==totalRow-1)&&[direction isEqual:@"From"])
__weak TripInfoListCell *weakCell = cell;
cell.PinImageView.image=[UIImage imageNamed:@"pin7@2x.png"];
cell.userNameLabel.text=loggedUser.profile_name;
cell.riderDriverSlNo.text=@"Driver";
if([drive_status isEqual:@"Ride"])
cell.riderDriverSlNo.text=@"Driver";
cell.userNameLabel.text=@"Driver"; //In case of "Rider" "From" name in the last row
else
cell.availabiltyInfoLabel.text=@"You";
NSString *imagePic = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_image"];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:imagePic]];
[cell.userImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
weakCell.userImageView.image=image;
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
NSLog(@"%@",error);
];
else if((indexPath.row==totalRow-1)&&[direction isEqual:@"To"])
cell.PinImageView.image=[UIImage imageNamed:@"pin7@2x.png"];
cell.userNameLabel.text=[_tripDetails objectForKey:@"park_name"];
NSString *image=[NSString stringWithFormat:@"%@",[dict123 objectForKey:@"parkBannerImageUrl"]];
[[ImageHandler sharedInstance] getImageFromURL:image withCompletion:^(UIImage *image)
if (image)
[cell.userImageView setImage:image];
];
else
if ([tableGoersList count] > indexPath.row)
NSDictionary *goersInfo=[tableGoersList objectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults]setObject:[goersInfo valueForKey:@"user_image_url"] forKey:@"user_image"];
[[NSUserDefaults standardUserDefaults]synchronize];
__weak TripInfoListCell *weakCell = cell;
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:goersInfo[@"user_image_url"]]];
[cell.userImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
weakCell.userImageView.image=image;
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
NSLog(@"%@",error);
];
if((indexPath.row==0)&&[direction isEqual:@"To"])
if([drive_status isEqual:@"Drive"])
cell.riderDriverSlNo.text=@"Driver";
cell.PinImageView.image=[UIImage imageNamed:@"pin1@2x.png"];
cell.userNameLabel.text=loggedUser.profile_name;
cell.availabiltyInfoLabel.text=@"You";
else
else if((indexPath.row==0)&&[direction isEqual:@"From"])
if([drive_status isEqual:@"Drive"])
cell.PinImageView.image=[UIImage imageNamed:@"pin1@2x.png"];
cell.userNameLabel.text=[_tripDetails objectForKey:@"park_name"];
cell.riderDriverSlNo.text=@"";
cell.availabiltyInfoLabel.text=@"";
NSString *image=[NSString stringWithFormat:@"%@",[dict123 objectForKey:@"parkBannerImageUrl"]];
[[ImageHandler sharedInstance] getImageFromURL:image withCompletion:^(UIImage *image)
if (image)
[cell.userImageView setImage:image];
];
else
NSLog(@"rider from");
cell.riderDriverSlNo.text=@"";
else
imageNameArray = [[NSArray alloc] initWithObjects:@"pin2@2x.png", @"pin3@2x.png", @"pin4@2x.png", @"pin5@2x.png", @"pin6@2x.png",@"pin8@2x.png",@"pin9@2x.png", nil];
NSString* nameStr=[goersInfo valueForKey:@"name"];
NSArray * nameStrArray = [nameStr componentsSeparatedByString: @"'s"];
nameStr=[nameStrArray objectAtIndex:0];
cell.riderDriverSlNo.text=[NSString stringWithFormat:@"Rider %ld",(long)indexPath.row];
index = [TripInfoVC ifNameExists:_arrName Name:nameStr];
if(index == 0)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:0]];
[_arrName addObject:nameStr];
if(index == 1)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:1]];
if(index == 2)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:2]];
if(index == 3)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:3]];
if(index == 4)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:4]];
if(index == 5)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:5]];
if(index == 6)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:6]];
if(index == 7)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:0]];
if(index == 8)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:1]];
if(index == 9)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:2]];
if(index == 10)
cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:3]];
// this pinImageView is the pin image which is repeated on scrolling
return cell;
请帮我解决这个问题。
【问题讨论】:
【参考方案1】:你可能会面临关闭碰撞。当你声明完成块时
[cell.userImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
weakCell.userImageView.image=image;
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
NSLog(@"%@",error);
];
weakCell 是指向 UITableView 委托请求的单元格对象的指针。当您滚动表格时,它可能会在稍后重用,此时您的完成块实际上可能会被调用,因为下载 进程不是立即的。
如何处理?我会使用 UITableViewCell.tag 等属性来识别我在需要单元格时请求的确切图像,并且它是否与我在完成块运行时尝试放入该单元格的图像匹配。可能稍后会触发其他下载并覆盖标签属性。
【讨论】:
,问题出在 "cell.PinImageView.image=[UIImage imageNamed:@"pin7@2x.png"];"此 PinImageView 正在从数组中获取图像【参考方案2】:您必须通过将代码添加到操作队列上的 UICollection cellForRowAtIndexPath 方法的委托方法中来实现动态加载图像或延迟加载。
而且,如果你在 UI 上做一些你放在主线程中的事情,这里是另一个例子:
使用 SDWebImageManager 在 UICollectionView 中延迟加载图像
【讨论】:
以上是关于由于可重用性问题,tableview 单元格上的图像重复的主要内容,如果未能解决你的问题,请参考以下文章
最后一个单元格上的 UITableView beginUpdate 和 endUpdate 重新加载 tableview
如何使表格视图单元格上的按钮调用正确的 tableView:cellForRowAtIndexPath?
在目标 c 中按住 tableview 单元格上的按钮状态单击