2012-12-31 92 views
1

我想异步加载图像在UITableVIew在uitableviewcell异步加载图像

- (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后,启动异步线程下载图像,在下载最新的图像之前,回滚到顶部,也许是第一页。一段时间后,下载一些图像,它们应该显示在那里,但现在图像将覆盖当前页面图像,也许猫图像现在有一个狗形象)我该怎么办?

回答

1

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

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

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

+0

是的,是的,是的,你完全正确@rob !!!!!但我不太明白“用它来查询当前显示该行的单元格的表视图(通过发送cellForRowAtIndexPath:到表视图)”。你能告诉我一些示例代码吗? –

+0

嘿@rob我明白了!哦,你帮了很多,感谢哥们! –