Alex Bollbach
Alex Bollbach

Reputation: 4570

CollectionView datasource method called with incorrect CollectionView argument

I have two collection views as properties of a custom view as properties. So lets call them self.collViewA and self.collViewB. When implementing datasource methods, I use if statements to configure the correct collection view with the correct information. I simply use isEqual: to check the collectionView parameter to each datasource callback. This works for every datasource callback except for sizeForItemAtIndexPath.

My implementation is as follows:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

   if ([collectionView isEqual:self.collViewA]) {
            return CGSizeMake(25,25);
   }
   if ([collectionView isEqual:self.collViewB]) {
      return CGSizeMake(50,50);
   }
   return CGSizeMake(10,10);
}

The problem seems to be that when both collection views are instantiated in my setup method (called after initializing the custom view that is the super of these collection views), only the second if statement passes and both collection views have 50,50 sized cells. If I remove the code that instantiates the second collection view, than the first if WILL pass and I'll get 25,25 sized cells for the right view, but obviously no second collection view.

What I don't get is how I use the same conditional logic with isEqual: on every other callback and have no problems.

Upvotes: 1

Views: 43

Answers (1)

Tim007
Tim007

Reputation: 2557

You can use tag to solve that

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

   if (collectionView.tag == 1) {
            return CGSizeMake(25,25);
   }
   if (collectionView.tag == 2) {
      return CGSizeMake(50,50);
   }
   return CGSizeMake(10,10);
}

Upvotes: 1

Related Questions