3

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

user845978
  • 217
  • 4
  • 10
  • Try reloading your horizontal collectionView inside the call for cellForRow for the vertical collection view – Aravind A R Jul 24 '17 at 06:27
  • Manually call `reloadData` on `collectionView`. – Priyal Jul 24 '17 at 06:33
  • Tried reloading data in cellForRow for the vertical and inside awakeFromNib in the verticalCell and I get "fatal error: unexpectedly found nil while unwrapping an Optional value " – user845978 Jul 24 '17 at 07:39

2 Answers2

4

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.

  • I have a table view that the cell of that table view has a horizontal paging collection view and In this collection view there is another collection view But the problem is that when I change the table view cell height the collection view won't fit to the cell But I used constraints in story board – Saeed Rahmatolahi Apr 16 '18 at 05:06
  • if the collectionView does not fit, along with the constraint(which I assume you gave correct constraints) try implementing these 4 delegate functions for collectionView and give the size of cell according to the height of tableView `sizeForItemAt indexPath: IndexPath -> CGSize {} minimumLineSpacingForSectionAt section: Int -> CGFloat {} minimumInteritemSpacingForSectionAt section: Int -> CGFloat {} insetForSectionAt section: Int) -> UIEdgeInsets {}` – Zamzam Pooya Apr 16 '18 at 05:24
  • so here is all my codes can you help me please to do that ? https://stackoverflow.com/questions/49850234/the-collection-view-inside-table-view-cells-wont-fit-to-the-cell-even-after-con – Saeed Rahmatolahi Apr 16 '18 at 05:38
1

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/

Rillieux
  • 587
  • 9
  • 23