Re: Can't get dynamically sized Collection View cells
Re: Can't get dynamically sized Collection View cells
- Subject: Re: Can't get dynamically sized Collection View cells
- From: Peter Tomaselli <email@hidden>
- Date: Wed, 30 Sep 2015 18:54:14 -0400
Certainly! Just to be clear (ended up revisiting some of my research on this today), I don't think it is possible to do this with just estimatedItemSize alone.
So, the analogy with UITableView w/r/t "self sizing", as presented or at least strongly implied in that WWDC talk, breaks down here—you also need a "sizing cell" or some other way of performing the auto layout measurements yourself and then providing them to the delegate.
I would love to be told otherwise here! But I don't believe I’ve ever seen it done.
Peter
> On Sep 30, 2015, at 5:41 PM, Doug Hill <email@hidden> wrote:
>
> Peter,
>
> Thanks for the reply and the code sample. It definitely has a lot of cool stuff.
>
> However, I’m still interested in whether anyone else has info in on dynamically-sized Collection View cells using the ‘estimatedItemSize’ property of the flow layout. Is this a lie from a WWDC presenter? Are there a number of other steps that aren’t documented that one needs to do? Any Apple sample code that implements this?
>
> Apple experts please chime in!
>
> Doug Hill
>
>> On Sep 29, 2015, at 7:12 PM, Peter Tomaselli <email@hidden> wrote:
>>
>> Hi Doug! Funny you mention this, I was trying and failing to do the same thing just tonight.
>>
>> Thing is, I’ve done it before and have a toy implementation on GitHub[0] to prove it! Not that that was helping me just now, of course, but perhaps a link to that repo can help you out?
>>
>> There are additional layers of complication in the linked project because 1. the labels inside the cells are themselves line-wrapping if necessary, so it’s quite “inside-out” as far as layout goes, and 2. the flow layout is “left-justified” instead of “fully-justified”, so be sure to ignore those aspects of the code if you do end up peeking.
>>
>> Anyway, to be brutally honest I can’t for the life of me remember which of the methods and/or properties in this old project are critical for getting your scenario to work, and which are related solely to mine, but perhaps it’s a decent starting point anyway.
>>
>> IIRC there also was a gotcha here wherein setting up your cell in IB basically meant you were SOL as far getting everything to work went (something about subviews not being embedded in the contentView properly?), but I could be wrong about that.
>>
>> Anyway a truly flowy layout is one of the things I most desire so if you gain any insights more, or if anyone else can chime in with better advice, I am very interested too.
>>
>> — Peter
>>
>> [0] https://github.com/Peterbing/CV-AutoLayout
>>
>>> On Sep 29, 2015, at 6:29 PM, Doug Hill <email@hidden> wrote:
>>>
>>> I’m trying to implement a collection with dynamically sized cells. The WWDC session from 2014 “What’s New in Table and Collection Views” talks about how to do this by:
>>> • Use autolayout constraints to set the height of the cell based on the content size of the subviews.
>>> • Set the estimatedItemSize property of the flow layout object to get the scrollbars to be in close the correct location.
>>>
>>> This doesn’t seem to work as I see the following log message:
>>>
>>> the behavior of the UICollectionViewFlowLayout is not defined because:
>>> the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
>>> Please check the values return by the delegate.
>>> The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7c218e20>, and it is attached to <UICollectionView: 0x7a411c00; frame = (0 0; 205 615); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7c7674f0>; layer = <CALayer: 0x79e25060>; contentOffset: {0, 0}; contentSize: {205, 66}> collection view layout: <UICollectionViewFlowLayout: 0x7c218e20>.
>>> 2015-09-29 14:48:29.255 Chartcube[65578:3340001] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
>>>
>>> also
>>> the behavior of the UICollectionViewFlowLayout is not defined because:
>>> the item width must be less than the width of the UICollectionView minus the section insets left and right values.
>>> Please check the values return by the delegate.
>>>
>>> FWIW I don’t set any other properties of the flow layout.
>>>
>>> Doing a Google search leads me to the following page:
>>>
>>> http://corsarus.com/2015/collection-view-with-self-sizing-cells/
>>>
>>> which discusses this error message and how to add another constraint at runtime to fix it. It sets a fixed width constraint of the cell content view. This “solution” appears to be a bit of a hack since they did some trial and error to figure out how to set it so that it would make things work. This leads me to believe there’s something else going on. Also, it would be great to know why I get the error in the first place. Maybe there’s some other setting in the flow layout that needs to be made?
>>>
>>> Can someone comment on how to implement dynamically-sized collection view cells correctly? Unfortunately, the WWDC session doesn’t go into detail on how they got their collection view to autoresize the cells and there’s isn’t any sample code for the project described in the video. It would be great to know the “approved” way to implement this behavior.
>>>
>>> Thanks!
>>>
>>> Doug Hill
>>>
>>> _______________________________________________
>>>
>>> Cocoa-dev mailing list (email@hidden)
>>>
>>> Please do not post admin requests or moderator comments to the list.
>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden