问题描述
我在使用SDWebImage将图像加载到自定义UITableViewCell内的UIImageView时遇到问题。
这是我在UITableView委托的代码:
i'm having an issue using SDWebImage to load images to a UIImageView inside a custom UITableViewCell.This is my code at the UITableView delegate:
static NSString *userTableId = @"userTableId";
UserDetailsTableViewCell *cell = (UserDetailsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:userTableId];
NSDictionary *user = [userList objectAtIndex:indexPath.row];
NSInteger position = indexPath.row + 1;
[cell.userDetailsView loadFromDictionary:user WithIndex:position ForCharitie:false];
cell.userDetailsView.delegate = self;
cell.userDetailsView.delegate = self;
return cell;
这是我的loadFromDictionary代码:
And here is my code for loadFromDictionary:
-(void) loadFromDictionary: (NSDictionary *) dic WithIndex: (NSInteger) index ForCharitie: (BOOL) isCharitie{
NSDictionary *userImageDic = [dic objectForKey:@"image"];
NSString *url =[userImageDic objectForKey:@"url"];
[userImage setImage:[UIImage imageNamed:@"defaultAvatar"]];
if ([url class] != [NSNull class]){
[userImage setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"defaultAvatar"]];
}
}
现在,问题是如果我在某些之前向下滚动图像完成加载。例如,假设我看到前8行,第1,2和3行仍然加载他们的图像,现在我滚动到9-16,我看到 defaultAvatar
所有这些,几秒后(我猜第1,2和3行的图像完成下载),单元格9,10和11上的图像变为属于1,2和3的图像。
我不知道是否有办法在重复使用单元格时停止下载图像,或类似的东西。
谢谢你,对不起我的英文!
Now, the problem is if i scroll down before some images finish loading. For example, let's say I see the first 8 rows, and rows 1, 2 and 3 still loading their images, now i scroll to 9-16, i see the defaultAvatar
for all of them, and after a few seconds (i guess when images of rows 1,2 and 3 finish downloading), the images on cells 9, 10 and 11 change to the ones that belong to 1,2 and 3.I don't know if there is a way to stop the images from downloading when i reuse the cell, or something like that.Thank you and sorry for my English!
推荐答案
如果您在桌面上设置了UITableViewDelegate,则可以使用委托方法:
If you have your UITableViewDelegate set on your table, you could use the delegate method:
在单元格滚出屏幕时将图像设置为NULL(或默认值)。
to set the image to NULL (or the default) when your cell scrolls off screen.
自从你使用SDWebImage,取消它可以像。
And since you're using SDWebImage, canceling it could be as easy as "cancelCurrentImageLoad
" on the cell's image view.
这篇关于SDWebImage + UITableViewCell,滚动时图像错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!