【发布时间】:2017-08-24 12:22:45
【问题描述】:
我希望能够在sizeForItemAtIndexPath 函数中调用我的UICollectionViewCell 类。像这样:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cell = MyCollectionViewCell()
let itemHeights = cell.titleLabel.frame.size.height + cell.subtitleLabel.frame.size.height + 20
return CGSize(width: self.view.frame.size.width - 30, height: itemHeights + cell.thumbnail.frame.size.height)
}
问题是cell.titleLabel.frame.size.height、cell.subtitleLabel.frame.size.height 和cell.thumbnail.frame.size.height 都返回nil。我认为这是因为每当调用 sizeForItemAtIndexPath 时,单元格还没有加载,cellForItemAtIndexPath 还没有被调用。
我需要知道这一点,因为cell.titleLabel 可以是在cellForItemAtIndexPath 中设置的多条线和宽度。
有什么想法吗?
【问题讨论】:
-
您不应该在除
cellForItemAt:之外的函数中将单元格出列即使您确实将单元格出列,您也不会得到一个填充了该索引路径值的单元格。您需要根据单元格的数据计算高度值,或使用自动单元格高度并为您的单元格提供合理的estimatedSize
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout