0

I have 2 collection views. For first collection view I just see one cell per time, but for second collection view I see 8 cells per time.

So for second collection view everything works ok, but first collection view duplicate some cells.

If I use this code below it cause some issues with duplicated cells. I am not sure that the cells are duplicated, but each my cell contains image view that show an image. So images in those cells are duplicate 100% guaranty.

- (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;
}

I have look at the call back above and I have added instance for my first collection view that is also top collection view in my superview hierarchy. Now the source code looks like this:

- (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;
    }
}

I am not sure why I need to check collection view, but it works right now. Each cell displays right content. So there is no duplicated issues with first collection view.

In additional I can add code how I setup image:

- (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) {

        }];
}

But I don't know why I need to check collection view in call back, because actually it does the same as for first collection view as for second collection view. For loading image I use AFNetworking feature.

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

1 Answers1

0

Sounds like a cell reuse issue. Do you remove your theImageView in the prepareForReuse function of your customCell ?

-(void) prepareForReuse {

    [super prepareForReuse];

    [self.theImageView removeFromSuperview];
    self.theImageView = nil;

    [self.indicator removeFromSuperview];
    self.indicator = nil;
}

I made a clearer answer about this issue on an other post : UICollectionView adding image to a cell

I might be wrong about the nature of your problem though. Let me know !

Community
  • 1
  • 1
Kujey
  • 1,122
  • 6
  • 17
  • thank you for reply, I have disabled autaolayout and size classes and now everthing works ok. I don't know how this affect on it, but it cause many issues in my project, i mean about auto layout and size classes – Matrosov Oleksandr Dec 23 '14 at 15:48