I am trying to implement a collection view inside each table view cell in a table view, but am having trouble getting the collection view to reload at the right time. It looks l
You cannot rely on the sequence of the calls. Currently you are trying to pass setIndex
from the table view's cellForRowAtIndexPath
to the collection view's delegate methods. A simple way to fix this is rather than using a single variable to pass the row number, instead pass it in the collection view's tag property. Then each collection view will know it's relevant row number. i.e.
In the table view's cellForRowAtIndexPath
:
cell.collectionView.tag = indexPath.row
And then in the collection view's numberOfItemsInSection
:
return self.model.sets[collectionView.tag].subsets!.count
Note also that you should not need to test dataIsReady
in these methods. This code is only needed in the table view's tableView:numberOfRowsInSection:
. When the data is not ready, it should return 0 (ie. no rows to display). Therefore the table view's cellForRowAtIndexPath
will never be called, and since there are no rows, there are no collection views, so their numberOfItemsInSection
will never be called.