Reloading collection view inside a table view cell happens after all cells in table view have been loaded

后端 未结 1 487
遇见更好的自我
遇见更好的自我 2021-01-27 02:01

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

相关标签:
1条回答
  • 2021-01-27 02:44

    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.

    0 讨论(0)
提交回复
热议问题