Search code examples
swiftuitableviewuicollectionviewuicollectionviewcellreloaddata

Reloading TableView from CollectionViewCell


Does anyone have idea how to reload TableView that's inside of CollectionViewCustomCell? I have an IBOutlet of that Table and the only way to access that Table is inside cellForItemAtIndexPath method where I can access my CollectionViewCustomCell and through that access the Table.

cell.tableViewInsideOfCollectionViewCell.reloadData()

But this only works on the first load, when I change CollectionView source it will not reload TableView.

If you need anymore info, please ask!

Thanks in advance!


Solution

  • You can get the cell using collectionView.cellForItem(at: IndexPath) method. Then cast it to the type of cell that contains the tableView. Now you have access to that cells properties and can reload the tableView.

    guard let cell = collectionView.cellForItem(at: IndexPath(row: , section: )) as? CustomCellWithTableView else { return }
    cell.tableView.reloadData()