在uitableviewcell中asynchronous加载图像

我正在尝试在UITableVIewasynchronous加载图像。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = nil; NSString *cellIndetify = [NSString stringWithFormat:@"cell%d",tableView.tag -TABLEVIEWTAG]; cell = [tableView dequeueReusableCellWithIdentifier:cellIndetify]; IndexPath *_indextPath = [IndexPath initWithRow:indexPath.row withColumn:tableView.tag - TABLEVIEWTAG]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.contentView.backgroundColor = [UIColor blackColor]; // here I init cell image view UIView *cellSub = [self.waterFlowViewDatasource waterFlowView:self cellForRowAtIndexPath:_indextPath]; cellSub.backgroundColor = [UIColor blackColor]; [cell.contentView addSubview:cellSub]; cellSub.tag = CELLSUBVIEWTAG; } // fill image info [self.waterFlowViewDatasource waterFlowView:self fillDataIntoCellSubview:[cell viewWithTag:CELLSUBVIEWTAG] withIndexPath:_indextPath]; CGFloat cellHeight = [self.waterFlowViewDelegate waterFlowView:self heightForRowAtIndexPath:_indextPath]; CGRect cellRect = CGRectMake(0, 0, _cellWidth, cellHeight); [[cell viewWithTag:CELLSUBVIEWTAG] setFrame:cellRect]; [self.waterFlowViewDatasource waterFlowView:self relayoutCellSubview:[cell viewWithTag:CELLSUBVIEWTAG] withIndexPath:_indextPath]; return cell; } 

在那ImageView类我init:

 (id)initWithIdentifier:(NSString *)indentifier { if(self = [super initWithIdentifier:indentifier]) { self.backgroundColor = [UIColor blackColor]; if (!self.imageView) { _imageView = [[UIImageView alloc] init]; self.imageView.backgroundColor = IMAGEVIEWBG; [self addSubview:self.imageView]; self.imageView.layer.borderWidth = 1; self.imageView.layer.borderColor = [[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0] CGColor]; } ......some others controllers } -(void)setImageWithURL:(NSURL *)imageUrl{ UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromKey:[imageUrl absoluteString]]; if (cachedImage) { self.imageView.image = cachedImage; }else{ SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadWithURL:imageUrl delegate:self options:SDWebImageCacheMemoryOnly userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[imageUrl absoluteString], @"image_url", [NSValue valueWithBytes:&imageSize objCType:@encode(CGSize)], @"image_size", [NSNumber numberWithInt:arrIndex], @"imageview_index", nil]]; _imageView.image = [UIImage imageNamed:@"album_detail_placeholder.png"]; } ..... ..... - (void)imageDecoder:(SDWebImageDecoder *)decoder didFinishDecodingImage:(UIImage *)image userInfo:(NSDictionary *)userInfo { imageView.image = image } 

正如你在这里看到我使用SDWebImageManager,但这似乎并不是问题。

当下拉到下一页时,在下一页的图像加载之前,如果我滚动回到上一页的加载图像,图像将被另一图像覆盖(应当显示在最新页面中,而不是当前页面上) (例如,在第一页,我有一个图像,这个图像中有猫,然后向下滚动,直到请求下一页,获得所有下一页图像的url后,启动asynchronous线程下载图像,之前下载最新的图片,回到顶部,也许是第一页,过了一段时间,下载了一些图片,它们应该显示在那里,但是现在图片会覆盖当前的页面图片,也许猫图片上现在有一个狗图片) 我该怎么办?

当一行在屏幕外滚动时,表格视图重复使用该行的单元格作为在屏幕上滚动的另一行。

问题是您正在使用ImageView作为后台加载器的ImageView ,而ImageView与单元格关联,而不是行。 当背景加载器完成加载图像时,单元格可能已被重新用于另一行。

您需要使表视图的数据源成为背景图像加载器的代表。 当它调用downloadWithURL:delegate:options:userInfo:时,它应该把行的索引path放到userInfo字典中。 当下载器发送imageDecoder:didFinishDecodingImage:userInfo:到数据源时,数据源应该从userInfo获取索引path,并使用它向当前显示该行的单元格询问表视图(通过发送cellForRowAtIndexPath:到表视图)。