【问题标题】:2 UICollectionView issues with data source2 UICollectionView 数据源问题
【发布时间】:2014-12-22 12:47:34
【问题描述】:

我有 2 个集合视图。对于第一个集合视图,我每次只看到一个单元格,但对于第二个集合视图,我每次看到 8 个单元格。

所以对于第二个集合视图,一切正常,但第一个集合视图重复了一些单元格。

如果我在下面使用此代码,则会导致重复单元格出现一些问题。我不确定单元格是否重复,但我的每个单元格都包含显示图像的图像视图。因此,这些单元格中的图像是 100% 保证重复的。

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

    NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];

    [collectionViewCell loadImageWithURLString:artwork[@"image_url"]];

    return collectionViewCell;
}

我查看了上面的回调,并为我的第一个集合视图添加了实例,该集合视图也是我的超级视图层次结构中的顶级集合视图。现在源代码如下所示:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.collectionViewTop])
    {
        GalleryCollectionViewCell *collectionViewCell = (GalleryCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];

        NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];

        [collectionViewCell loadImageWithURLString:artwork[@"image_url"]];

        return collectionViewCell;
    }
    else
    {
        GalleryCollectionViewCell *collectionViewCell = (GalleryCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCell" forIndexPath:indexPath];

        NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];

        [collectionViewCell loadImageWithURLString:artwork[@"image_url"]];

        return collectionViewCell;
    }
}

我不确定为什么需要检查集合视图,但它现在可以工作。每个单元格显示正确的内容。所以第一个集合视图没有重复的问题。

另外,我可以添加代码如何设置图像:

- (void)loadImageWithURLString:(NSString *)urlString
{
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

        [self.theImageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"home_screen_logo"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

            [self.theImageView setImage:image];

            [self.indicator stopAnimating];

            [self.indicator setHidden:YES];

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

        }];
}

但我不知道为什么我需要在回调中检查集合视图,因为实际上它与第一个集合视图和第二个集合视图的作用相同。对于加载图像,我使用AFNetworking 功能。

【问题讨论】:

    标签: ios uicollectionview uicollectionviewcell afnetworking-2


    【解决方案1】:

    听起来像是单元重用问题。 您是否在 customCell 的 prepareForReuse 函数中删除了 theImageView ?

    -(void) prepareForReuse {
    
        [super prepareForReuse];
    
        [self.theImageView removeFromSuperview];
        self.theImageView = nil;
    
        [self.indicator removeFromSuperview];
        self.indicator = nil;
    }
    

    我在另一篇文章中对这个问题做了更明确的回答:UICollectionView adding image to a cell

    不过,我可能对您的问题的性质有误。告诉我!

    【讨论】:

    • 感谢您的回复,我已禁用自动布局和大小类,现在一切正常。我不知道这对它有什么影响,但它会在我的项目中引起很多问题,我的意思是关于自动布局和大小类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多