Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: UICollectionViewCell object is always null in sizeForItemAtIndexPath

I have a collection view that contains different custom cells.

These cells contain different content that varies in size.

The cells are defined in the Storyboard, so no registering needed in code.

All i want to do now is change the size in the delegate method depending on the content:

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

id cellAtIndexPath = [collectionView cellForItemAtIndexPath:indexPath];

NSLog(@"The cell: %@", cellAtIndexPath);

...
e.g. calling sizeToFit methods and cumulating the sizes of the cells' subviews
...

}

The Method is called as expected for every cell, but cellAtIndexPath always returns NULL, no matter what i tried. So i am not able to access the cell object at the indexPath or its content view.

Any suggestions why i cannot access the cell object?

like image 440
crud21 Avatar asked Nov 05 '12 11:11

crud21


1 Answers

The collectionView:layout:sizeForItemAtIndexPath method is called by the collection view's flow layout object. The layout is asking for the size of a cell at a time before the cells are added to the collection view. You will have to compute the desired cell size "on your own" and return it.

like image 57
chris Avatar answered Oct 23 '22 22:10

chris