Collection-View-in-a-Table-View-Cell
Collection-View-in-a-Table-View-Cell copied to clipboard
Reusable collection view cells with `flowLayout.scrollDirection = .vertical`
I have a similar setup with a collection view inside each table view cell. Table view cells' heights are calculated dynamically using auto layout.
Sometimes one of these embedded collection view has to behave like a table view. i.e. flowLayout.scrollDirection = .vertical
. Like in iOS 10 Music app, some tiles scroll horizontally while others are flattened (?) to scroll with the table view. I'm addressing the latter by just changing the flow direction to vertical.
For these vertical direction collection views, I calculate and apply the height constraint manually. It works. However, all of the collection view cells get loaded altogether in the memory, as soon as the containing table view cell enters the screen.
One "fix" I can imagine is to see if a collection view cell's bounds overlap visible bounds and then "actually" render its model. This would be a less desired workaround but could potentially be a good enough solution in absence of an alternative.
Do you have a suggestion to make them respect their view-port/enable them to be reusable like they would if table view wasn't in the hierarchy?
Hey! 👋
Yikes, that sounds tricky. I've never gotten autolayout-driven dynamic cell sizes to work, I just always calculate by hand (it sucks, but it's reliable). If it were me, I'd use that approach until it became a memory issue, so just profile on a slow device to double-check 👍