• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UICollectionViewController 在滚动时重新加载图像

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:57:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap