0

我在我的实现中添加了这个 UICollectionViewDelegate:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SAPCollectionViewCell *collectionViewCell = (SAPCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SAPCollectionViewCell" forIndexPath:indexPath];

    NSInteger index = indexPath.item + indexPath.section * 2;

    NSString *imagePath = [_images objectAtIndex:index];

    collectionViewCell.index = index;

    [collectionViewCell setImageWithPath:imagePath];

    collectionViewCell.delegateCell = self;

    return collectionViewCell;
}

在我的自定义单元格中,我有这个方法:

- (void)setImageWithPath:(NSString *)imagePath
{
    if (!self.imageView)
    {
        CGRect rect = self.contentView.frame;

        self.imageView = [[UIImageView alloc] initWithFrame:rect];

        [self.contentView addSubview:self.imageView];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            UIImage *image = [UIImage imageFromPath:imagePath];

            UIImage *resizedImage = [UIImage imageWithImage:image scaledToWidth:rect.size.width];

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.imageView setImage:resizedImage];

            });
        });
    }
}

因此,您可以看到单元格是否没有 UIImageView,然后我开始创建它,并在后台从本地路径获取图像并将其调整为单元格内容视图宽度,然后在主队列中将图像设置为 UIImageView。

这部分效果很好,但是当我尝试滚动 UICollectionView 时,例如使用 10 张图像,我注意到,例如,如果当我向下滚动时前 2 个顶部图像消失,然后当我滚动回顶部时,图像会被更改放置。

这是向下滚动之前的图像状态:

在此处输入图像描述

我滚动UIColectionView回顶部后的这种状态:

在此处输入图像描述

因此,您可以首先看到项目更改了它们的位置。正如我if (!self.imageView)在上面的代码中设置的那样,它应该意味着图像永远不会被创建两次或更多次。

我打开调试器并正在检查这个方法:

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

因此,当 UICollectionView 第一次创建单元格时,图像路径会一一返回。

第一次第一个 UICollectionViewCell 有 0x8fbc1d0,第二个有 0x8d0a4c0 地址

但是当我向下滚动然后向上滚动时,调试器在第一个 0x8d0a4c0 向我显示了第二个地址,然后向我显示了 0x8fbc1d0

但我不明白为什么物品会改变它们的顺序。或者也许这是另一个问题?

此外,我在代码中没有任何方法可以让我重新排序单元格。只有少数 UICollection 视图方法委托配置单元格计数并创建它们。

此外,如果我删除if (!self.imageView)似乎一切正常,但是我的代码每次调用调度对我都没有好处,因为当我将图像调整为单元格大小时,我不需要执行两次。

4

2 回答 2

0
- (void)setImageWithPath:(NSString *)imagePath
{
    if (!self.imageView)
    {
        CGRect rect = self.contentView.frame;

        self.imageView = [[UIImageView alloc] initWithFrame:rect];

        [self.contentView addSubview:self.imageView];

    }

    if (![self.imagePath isEqualsToString:imagePath] && self.imageView.image == nil)
        {
            self.imagePath = imagePath;

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                UIImage *image = [UIImage imageFromPath:imagePath];

                UIImage *resizedImage = [UIImage imageWithImage:image scaledToWidth:rect.size.width];

                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([self.imagePath isEqualsToString:imagePath])
                    {
                        [self.imageView setImage:resizedImage];
                    }

                });

            });
        }
}
于 2014-09-04T23:14:21.307 回答
0

当您将这两个项目滚动到屏幕外时,您CollectionView会将关联的单元格放回“队列”。当您再次向上滚动时,dequeueResuableCellwithReuseIdentifierincellForItemAtIndexPath从该队列中检索它们,但(如您所见)不一定以相同的顺序。因此,项目 0 的单元格已用于项目 1,反之亦然。如您所见,if (!self.imageView)实际上阻止了正确的图像被加载。如果您想避免每次都检索和调整它们的大小,那么我会将(调整大小的)imageViews 存储在一个数组中。

于 2014-09-04T22:56:27.403 回答