1

我正在尝试将图像异步加载到UITableVIew.

- (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在我初始化的那个类中:

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

4

1 回答 1

1

当一行滚动到屏幕外时,表格视图会将该行的单元格重用于已在屏幕上滚动的另一行。

问题是您将ImageView用作后台加载程序的代表,并且ImageView与单元格相关联,而不是与行相关联。当后台加载程序完成加载图像时,该单元格可能已被另一行重用。

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

于 2012-12-31T17:53:41.763 回答