gpt4 book ai didi

swift - 重新加载时 UICollectionViewCell 的高度错误

转载 作者:可可西里 更新时间:2023-11-01 01:56:07 26 4
gpt4 key购买 nike

我将问题作为 this question 的一个分支

ViewCells 上的自动调整功能完美无缺。但是当我重新加载我的 CollectionView 时,高度返回 1。

代码如下。

extension SaleViewController: CollectionViewFlowLayoutDelegate{
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let flowLayout = self.myCollectionView.collectionViewLayout as? CustomLayout else {
return
}
flowLayout.invalidateLayout()
}

func height(at indexPath: IndexPath) -> CGFloat {
guard let cell = self.myCollectionView.cellForItem(at: indexPath) else {
return 1
}
cell.layoutIfNeeded()
//get calculated cell height
return cell.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
}
}

缺少什么或我还应该重新考虑什么以保持我的自动高度?

提前致谢。

最佳答案

检查你的 CustomLayout 会有缓存为你的 cell.ex 存储 layoutAttribute : private var cache: [IndexPath : UICollectionViewLayoutAttributes] = [:]

你必须在重新加载之前清除这个缓存

guard let flowLayout = self.myCollectionView.collectionViewLayout as? CustomLayout else {
return
}

flowLayout.cache.RemoveAll()
flowLayout.delegate = self
CollectionView.reloadData()

关于swift - 重新加载时 UICollectionViewCell 的高度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53365698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com