Question

Hello there stackoverflowers,

I have a collection view with a custom flow layout. My cells can have different sizes which are calculated by:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

Then, in every cell, I want to have a different layout depending on the available space. To do that in my

prepareForReuse 

I set the frame of every item according to the contentView.frame.size of the cell.

The problem is that the

self.contentView.frame.size

is not correct at that point. I think that the layoutAttributesForItemAtIndexPath: hasn´t been called yet.

So my question is, when can I trust the self.contentView.frame.size of a cell?

Thanks a lot, I hope I have explained myself properly.

Was it helpful?

Solution

The purpose of prepareForReuse is

to clean up, to prepare the view for use again. <...> to reset properties to their default values and generally make the view ready to use again.

Actually, layoutAttributesForItemAtIndexPath: is the place where you need to set appropriate cell sizes.

Moreover, custom collection view layout should implement collectionViewContentSize

Subclasses must override this method and use it to return the width and height of the collection view’s content. These values represent the width and height of all the content, not just the content that is currently visible. The collection view uses this information to configure its own content size for scrolling purposes.

That means, collection view layout should be aware of available space and adjust content appropriately (if your goal is to fit content)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top