【问题标题】:UICollectionView loads images in the wrong order for a secondUICollectionView 以错误的顺序加载图像一秒钟
【发布时间】:2015-03-05 14:01:53
【问题描述】:

我有一个带有自定义单元格的 UICollectionView,该单元格有一些标签和一个 UIImageView,数据是从一些 Json 下载的,然后我填充我的单元格,问题是当我再次向上滚动时,某些单元格的图像会发生一秒钟的变化进入错误的(另一种产品),然后他们再次重置为正确的。我正在为 UIImageView 使用延迟加载和以下库 https://github.com/nicklockwood/AsyncImageView 有没有人遇到我同样的问题? 我填充单元格的代码如下:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    [self performSelector:@selector(test) withObject:nil];

    cell.backgroundColor = [UIColor whiteColor];


    // Configure the cell
    FidelityProducts *item = _feedItems[indexPath.row];
    //load the image

    urlImg = [NSURL URLWithString:item.urlImg];
    cell.img.showActivityIndicator = true;
    cell.img.imageURL = urlImg;
    cell.titolo.text = item.nomeProdotto;
    cell.codice.text = item.codice;
    cell.prezzo.text = item.prezzo;

    UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
    bottomBorder.backgroundColor = [UIColor redColor];

    [cell.contentView addSubview:bottomBorder];


    return cell;
}

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    您需要在自定义UICollectionView 类中覆盖prepareForReuse,并将图像设置为nil。这应该可以解决您的问题:

    - (void)prepareForReuse {
        [super prepareForReuse];
    
        [self.img setImage:nil];
    }
    

    不太确定这种延迟加载 UIImageView 是如何工作的,也许您还需要将 imageUrl 设置为 nil

    希望它对你有用。祝你好运!

    【讨论】:

    • 试过但没有运气:(该方法被正确调用但错误仍然发生,无论如何感谢您的帮助!:)
    【解决方案2】:

    我知道你问已经有一段时间了,但我遇到了类似的问题,没有 LazyLoad。

    我用一些标识符设置了我的单元格的标签,后来我在调度队列中使用它来确保我正在加载同一个单元格。

    这是我的代码:

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cvCell";
    
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.backgroundColor=[UIColor colorWithRed:237.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1];
    for (UIView *vw in [cell subviews])
    {
        [vw removeFromSuperview];
    }
    
    
        NSURL *url = [NSURL URLWithString:[[self.dataArray objectAtIndex:indexPath.row] imageURL]];
        NSInteger evID =[[self.dataArray objectAtIndex:indexPath.row] ID].integerValue ;
        [cell setTag:evID];
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
    
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
            UIImageView *imagem = [[UIImageView alloc] initWithFrame:CGRectMake([GRAPHICS SCREEN_X], [GRAPHICS SCREEN_Y],106,106 )];
    
            dispatch_sync(dispatch_get_main_queue(), ^{
    //here is the trick for loading the right image
    
                if (cell.tag==evID) {
    
    
                [imagem setBackgroundColor:[UIColor clearColor]];
                [imagem setImage:image];
                [cell addSubview:imagem];
                [cell setNeedsLayout];
                }
    
            });
        });
    
    
    
    return cell;
    }
    

    希望这对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      相关资源
      最近更新 更多