skip to Main Content

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.

2

Answers


  1. 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().

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search