CollectionView nested inside Collectionview

*爱你&永不变心* 提交于 2019-12-12 23:12:23

问题


I have seen solutions where a collection view is nested inside a table view but for my app I need to have 2 collection views as it makes it easier to do some other things.

So lets call the root collection view VerticalCollectionView which only scrolls vertically and the nested collection view HorizontalCollectionView which only scrolls horizontally. I created them using the Storyboard. Below you'll see the orange is the Vertical with the green Horizontal with a label inside it.

And I have set the delegate & datasource of both collections to the same CollectionViewController.

I distinguish between the 2 different cells by checking which tableview the delegate method is referencing as such

My problem is that HorizontalCollectionView isn't getting instantiated. I have everything working for VerticalCollectionView, the background color, the number of items, etc. In the VerticalCollectionViewCell, I have an IBOutlet referencing HorizontalCollectionView


回答1:


I have used nested collectionViews in my recent app a lot. at first it didn't work for me but when I learned its away it became pretty simple.

instead of setting the HorizontalCollectionView delegate and dataSource to the same CollectionViewController make a UICollectionViewCell and set the delegates and dataSource in awakeFromNib() function of the cell and write the HorizontalCollectionView functions in that cell. it works like a charm ;)

if you needed more detailed answer let me know.




回答2:


In case someone is looking here since WWDC 19, Apple has introduced CompositionalLayout for UICollectionViews which make these tasks much much easier.

One should read here:

https://developer.apple.com/videos/play/wwdc2019/215/



来源:https://stackoverflow.com/questions/45273765/collectionview-nested-inside-collectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!