OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionViewController 在滚动时重新加载图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:57
标题: ios - UICollectionViewController 在滚动时重新加载图像

我正在使用 ParseFramework 获取一些图像并将它们显示在 collectionViewController 中。问题是,如果我在加载所有图像之前滚动,我会遇到图像重复,所以我会在不同的单元格中找到相同的图像。以下是对您有所帮助的代码片段。

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath {

    ExampleCell *cell = (ExampleCell*)[collectionView dequeueReusableCellWithReuseIdentifier"imageCell" forIndexPath:indexPath];

    PFObject * imageObject = [self.imageFilesArray objectAtIndex:indexPath.row];

    PFFile * imageFile=[imageObject objectForKey"image"];

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if(!error){
            cell.parseImage.image=[UIImage imageWithData:data];
        }
    }];
    return cell;
}

以及执行查询的方法:

-(void) queryParseMethod{

    PFQuery *query = [PFQuery queryWithClassName"Allimages"];
    [query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError *error) {
        if(!error){
            self.imageFilesArray = [[NSArray alloc] initWithArraybjects];
            [self.collectionView reloadData];
        }
    }];
}

这是在 ExampleCell.h 中

 @property (weak, nonatomic) IBOutlet UIImageView *parseImage;

这是在 ExampleCell.m 中

 @synthesize parseImage;

在 Storyboard 中,我将单元格的标识符设置为 imageCell

我考虑了下面的答案,所以我修改了我的方法:

 - (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
 {
  ExampleCell *cell = (ExampleCell*)[collectionView dequeueReusableCellWithReuseIdentifier"imageCell" forIndexPath:indexPath];

if(cell==nil) // no queued cell to dequeue
{
  cell = (ExampleCell *)[[UICollectionViewCell alloc] initWithFrame:CGRectMake(0,0,50,50)];
}

// clear any previous image if necessary
cell.parseImage.image = nil;

PFObject * imageObject = [self.imageFilesArray objectAtIndex:indexPath.row];
PFFile * imageFile=[imageObject objectForKey"image"];

[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
 {
     if(!error)
         cell.parseImage.image=[UIImage imageWithData:data];
 }];

return cell;

}

但我仍然有同样的问题。我做错了什么?



Best Answer-推荐答案


我认为您有重复的图像,因为某些单元格被重复使用。

当一个单元格不再可见时,它会被放入队列中,可以重复使用以显示新的单元格。单元格仍然保留对其图像的引用。

所以当你出列一个单元格时,你应该从清除它之前的图像开始:

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
{
    ExampleCell *cell = (ExampleCell*)[collectionView dequeueReusableCellWithReuseIdentifier"imageCell" forIndexPath:indexPath];

    if( !cell ) // no queued cell to dequeue
    {
            // create a new cell to use
    }

    // clear any previous image if necessary
    cell.parseImage.image = nil;

    PFObject * imageObject = [self.imageFilesArray objectAtIndex:indexPath.row];
    PFFile * imageFile=[imageObject objectForKey"image"];

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
    {
            if(!error)
                cell.parseImage.image=[UIImage imageWithData:data];
    }];

    return cell;
}

关于ios - UICollectionViewController 在滚动时重新加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016439/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4