Multiple UICollectionView in one controller

I have a view configured with two UICollectionViews. Each of these representations has an array supporting it with different sizes. collection1 is supported by array1, and collection2 is supported by array2. The problem is that the return number for collection1 from numberOfItemsInSection applies to both views of the collection.

For example, if array1 is 4 and array2 is 5, both collections will display 4 elements. If array1 is size 5 and array2 is size 4 when I look at collection2 the way it calls cellForItemAtIndexPath with itemIndex from 5 for collection2, and I get an NSRangeException.

How can I make each CollectionView my own size?

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; { if(view == self.colleciton1){ return self.array1.count; } else if (view == self.collection2){ return self.array2.count; } return 0; } - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; { if(cv == self.collection1){ CharacterCell *cell = [cv dequeueReusableCellWithReuseIdentifier:FIRST_CELL_IDENTIFIER forIndexPath:indexPath]; cell.label.text = self.array1[indexPath.item]; return cell; } else if (cv == self.collection2){ EpisodeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:SECOND_CELL_IDENTIFIER forIndexPath:indexPath]; cell.label.text = self.array2[indexPath.item]; return cell; } return nil; } 

I have included the git repository with a project illustrating the problem.

git @ github.com: civatrix / MultipleCollectionViews.git

+6
ios uicollectionview
Oct 12 '12 at 0:00
source share
3 answers

The problem was that I used the same layout object for each collection. In retrospect, this makes sense, but you have to make sure that you create different layouts for each CollectionView.

+19
Oct 12 '12 at 15:05
source share

It might be easier to use ContainerViews and have two separate UICollectionView controllers for each UICollectionView

+3
Jan 07 '13 at 9:53 on
source share

What you have to work. Are self.colleciton1 and self.collection2 IBOutlets? If so, can you check if they are connected correctly?

+1
Oct 12
source share



All Articles