IOS manages nested UICollectionViews issue

I ran into a problem while working with nested collection views. The hierarchy is as follows:

  • outerCollectionView
    • UIImageView
    • Label
    • innerCollectionView
      • Label
      • UIImageView

OuterCollectionView cells have screen size (works like scrolling pages) I set the delegate and data source outerCollectionView as a UIViewController and the delegate innerCollectionView and the data source as containing the cell as follows:

  • in ViewController.m

    -(FeaturedPageCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { FeaturedPageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FeaturedCell" forIndexPath:indexPath]; cell.nestedCollectionView.dataSource = cell; cell.nestedCollectionView.delegate = cell; [cell.nestedCollectionView setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:.01]]; cell.venueThumbnail.image = [UIImage imageNamed:[self.venueThumbnails objectAtIndex:indexPath.item]]; cell.venueNameLabel.text = [self.venueNames objectAtIndex:indexPath.item]; cell.venueNameLabel.textColor = [UIColor whiteColor]; cell.venueAddressLabel.text = [self.venueAddresses objectAtIndex:indexPath.item]; cell.venueAddressLabel.textColor = [UIColor whiteColor]; [cell setBackgroundColor:[[UIColor grayColor]colorWithAlphaComponent:.1]]; return cell; } 
  • in the implementation file for a cell containing innerCollectionView

     -(FeaturedEventCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { FeaturedEventCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FeaturedEventCellReuseIdentifier" forIndexPath:indexPath]; cell.eventDateLabel.text = [self.events objectAtIndex:indexPath.item]; cell.eventDateLabel.textColor = [UIColor whiteColor]; cell.eventImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Flyer%ld.png", self.venueNameLabel.text, (long)indexPath.item +1]]; return cell; } 

    In this block of code, I use the value of the contained cell label to load the data related to that cell into innerCollectionView.

Pretty simple implementation. As a result, I did not expect. Basically, what happens is that the data is loaded properly in the externalCollectionView cells, but not in the innerCollectionView cells (the very first ones load correctly, but not the rest). Also, scrolling elements in innerCollectionView will cause scrolling in other cells.

I thought that creating a delegate cell containing innerCollectionView would manage its own collection list independently of the others. I think I don’t understand something fundamental regarding collection views, reorientation and reuse.

Also nested collection views are a good way to implement my script? I tried to find an implementation of the nested representations of the collection and could not find a lot of things, maybe for some reason?

Please explain what I am doing wrong in my case, or direct me to the right or best solution.

+1
source share

All Articles