【发布时间】:2018-11-18 21:34:33
【问题描述】:
我将问题作为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
}
}
缺少什么或我应该重新考虑什么以保持我的自动高度?
提前致谢。
【问题讨论】:
-
因此,从这段代码来看,它似乎返回 1,因为它在您传递的 indexPath 中找不到单元格。我建议在
return 1行上放一个断点,并弄清楚为什么这个 indexPath 没有单元格 -
也许您必须执行
self.myCollectionView.reloadData()才能在访问高度之前重新加载数据。您可以尝试做的另一件事是在func cellForItem(at: IndexPath) -> UICollectionViewCell?函数中设置高度。要检查的另一件事是通过实现UICollectionViewDataSource并在初始化程序中使用self.datasource = self来查看您是否正确连接了数据源。 -
请发布您的自定义布局代码
标签: swift uicollectionviewcell autosize uicollectionviewflowlayout