CollectionView nested inside Collectionview

2019-07-31 02:58发布

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.

enter image description here

enter image description here

And I have set the delegate & datasource of both collections to the same CollectionViewController. enter image description here

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

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

enter image description here

enter image description here

1条回答
放我归山
2楼-- · 2019-07-31 03:37

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.

查看更多
登录 后发表回答