简体   繁体   中英

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

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.

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

您只能重新加载正在更新的单元格,以防您不想重新加载表格

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM