UICollection视图未重新加载单元格的图像视图



我在中调用了以下代码

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

不幸的是,当我调用[collectionView reloadItemsAtIndexPaths:@[indexPath]];时,中的imageView不会更新。如果我调用reloadData,它会。我正在努力保持代码尽可能高效,所以我不想只调用reloadData。有人能理解为什么这不起作用,或者我还需要做什么吗?在加载大量API图像查询时,我已经遇到了一些其他问题,这些问题与collectionView可爱的endAnimations内容相冲突。如有任何帮助,我们将不胜感激。

  SearchResultsCollectionViewCell *searchResultCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SearchResultsCollectionViewCell class]) forIndexPath:indexPath];
    Product *product = self.products[indexPath.row];
    UIImage *productImage = [self.imageCache imageForURL:product.fullImageUrl];

    if (productImage) {
        searchResultCell.productImageView.image = productImage;
    } else {
        [self.imageCache downloadImageForURL:product.fullImageUrl indexPath:indexPath withCompletion:^(UIImage *image, NSError *error) {
            if (!error) {
                 searchResultCell.productImageView.image = image;
//                [collectionView reloadData];
                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
            }
        }];
        UIImage* image = [UIImage imageNamed:@"001_logo"];
        searchResultCell.productImageView.image = image;
    }

访问和存储图像的方式似乎很奇怪。

我这样做的方法是为productImage上的UIImage创建一个@property

然后你可以询问产品的形象。如果产品返回零图像,则在单元格中放置占位符。

下载完成后,您可以重新加载单元格。

其次,我不会试图更改您正在进行的下载调用的完成块中的单元格。

完成块应该更新模型(即将图像存储到产品对象中),然后重新加载单元格。

这是因为你不确定你正在运行完成块的单元格是(a)与你启动它的单元格相同的单元格,还是(b)它仍然显示相同的产品。

最新更新