2

我将这个问题作为这个问题的一个分支

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
    }
}

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

提前致谢。

4

1 回答 1

1

检查您的 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()
于 2018-11-27T05:36:37.723 回答