【问题标题】:Repeated image loading in tableviewcell在 tableviewcell 中重复加载图像
【发布时间】:2014-12-18 09:30:15
【问题描述】:

我有一个表格视图,每个单元格都有自己的图像视图。我正在使用 AsyncImageView 将来自 Internet 的图像加载到 imageview 中。某些图像视图似乎将重复的图像加载到不同的单元格中:

在上面的屏幕截图中,图片 B 在不应该加载的情况下加载了两次。当我上下滚动时,这种情况经常发生。发生这种情况时,我滚动并滚动回单元格并加载正确的图像。后来当我再次滚动时,同样的问题发生了。我无法追踪问题的来源。有人有想法么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{
    static NSString *simpleTableIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }else
    {
        [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.logoImageView];
        cell.logoImageView.image = nil;
    }

    //Cause sections
    NSMutableArray *tempArray = [self.sectionArray objectAtIndex:indexPath.section];
    MyObject *myObject = [tempArray objectAtIndex:indexPath.row];

    NSString *thumbUrlString = [myObject valueForKey:@"thumbUrl"];

    if([thumbUrlString length]>0 )
    {
        NSString *imageUrl= [NSString stringWithFormat:@"http://www.sitename.com/%@",thumbUrlString];
        NSURL *url = [NSURL URLWithString:imageUrl];
        cell.logoImageView.imageURL=url;
    }else{
        cell.logoImageView.image = [UIImage imageNamed:@"placeholder.png"];
    }

    return cell;
}

【问题讨论】:

    标签: xcode uitableview tableview asyncimageview


    【解决方案1】:

    我想当您设置 cell.logoImageView.imageURL 时,您会在 CustomCell 代码或类似代码中调用 [AsyncImageLoader sharedLoader] loadImageAtURL:logoImageView.imageURL] 之类的东西。

    可能您对所有图像使用相同的共享 AsyncImageLoader,因此每次您请求新图像时都会覆盖先前的图像请求,或者您为每个请求创建不同的线程,因为它们将结果返回错误细胞。

    请发布您的 AsyncImageLoader 的一些代码。

    【讨论】:

    • 你好,albermala。我的 CustomCell 代码中没有任何内容,只有一个 nib 文件。我的印象是 AsyncImageView 根据他们的演示处理表格视图中的请求:github.com/nicklockwood/AsyncImageView/blob/master/Examples/… 你说的听起来很合理。因此,我是否必须创建不同的 AsyncImageLoader 实例?
    • @Moo33 你找到问题了吗?如果是这样,请将您的答案标记为正确答案,否则我们将继续讨论 AsyncImageLoader...
    【解决方案2】:

    犯了一个愚蠢的错误:

     cell.logoImageView.image = nil 
    

    改成:

    cell.logoImageView.imageURL = nil 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 2017-02-03
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多