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
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:
That will cause the table view to recalculate and update the row heights, without calling
.reloadData()
.You can reload only the cell that you are updating in case you don’t want to reload the table