CHTCollectionViewWaterfallLayout icon indicating copy to clipboard operation
CHTCollectionViewWaterfallLayout copied to clipboard

Utilize iOS 8 self-sizing cells to find out cell dimensions

Open hokster7 opened this issue 11 years ago • 6 comments

iOS 8 provides a new self-sizing API for CollectionView and CollectionViewCells. It lets cells determine their own height, based on the content that they're about to load.

This dance involves various methods like

  • preferredLayoutAttributesFittingAttributes: (on the cell)
  • shouldLayoutAttributesFittingAttributes: (on the layout)
  • invalidationContextForPreferredLayoutAttributes:withOriginalAttributes: (on the layout)

The waterfall layout could make use of this dance and harness the self-sizing height measurement, whilst demanding that width be fixed.

hokster7 avatar Sep 17 '14 09:09 hokster7

Try this solution http://stackoverflow.com/questions/25753373/ios-8-gm-does-not-update-constraints-on-collection-views

alobanov avatar Sep 19 '14 05:09 alobanov

After having a play around I'm not sure this is going to be possible, or the entire implementation may have to be overhauled.

By the time the self sizing cell calculates it's size (I think that's through preferredLayoutAttributesFittingAttributes:), prepareLayout has already been run, so there may be discrepancies in the frames if we're to use the new size, and the entire thing will need invalidating leading to a never ending cycle of the frames being wrong. Unless we could somehow cache the self size value to an index path and use that value if it exists.

Argh, I don't know, brain's dead, will have a crack another time.

adamwaite avatar Sep 29 '14 21:09 adamwaite

Caching the size based on the index path is a good idea. As you calculate a new size, only the cells below that cell need to be invalidated (and only their y positions, not their sizes). We do cell size caching based on index path in RZCellSizeManager. The only reason I'm not using it with this layout is because it doesn't play nice with trait collections, which is what led me to look into self-sizing cells.

ZevEisenberg avatar Dec 16 '14 17:12 ZevEisenberg

For me worked approach demoed here https://github.com/honghaoz/Dynamic-Collection-View-Cell-With-Auto-Layout-Demo. It is still produces heavy computations (and CPU spikes) on each realodData call.

Not sure if caching sizes will help in cases when content is rebuilt after user changes search criteria, sort order etc.

ikovalisko avatar Mar 15 '15 14:03 ikovalisko

@ikovalisko You were able to get CHTCollectionViewWaterfallLayout working with cell sizes from auto-layout? Were your cells defined in XIBs or as prototype cells in a storyboard? I've got the latter and can't get things working.

jalbano avatar Jan 30 '16 12:01 jalbano

Any movement on this since?

We have never been able to get the cells to not have broken constraints, and fast-forward years later self-sizing cells are the default that collection views utilize (opt-out by default)

Would be great to know that we can use this library still

marchy avatar Jan 06 '24 19:01 marchy