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.