Home > OS >  How to create dynamic collection view in a table view cell, without reloading table view swift?
How to create dynamic collection view in a table view cell, without reloading table view swift?

Time:07-09

I'm using this code in cellForRowAt :

cell.frame = tableView.bounds
    cell.layoutIfNeeded()
    collectionView.reloadData()
    heightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height

and

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

This works at the first time, but when I load more items to collection view it doesn't work unless I use tableView.reloadData()

But when I use tableView.reloadData() scrolling stops for a second, and I want it to flow without freezing.

Anyone has a solution? Thanks in advance.

CodePudding user response:

Any time you change a cell height after it has been displayed, you need to tell the table view about it.

You haven't shown how or when you "load more items to collection view" ... but after you do that, call:

tableView.performBatchUpdates(nil, completion: nil)

That will cause the table view to recalculate and update the row heights, without calling .reloadData().

CodePudding user response:

You can reload only the cell that you are updating in case you don't want to reload the table

    tableView.reloadRows(at: [IndexPath], with: .none)
  • Related