【发布时间】:2020-01-27 00:17:22
【问题描述】:
是否可以将 sizeForItemAt 代码包含为基于单元格类的动态代码?我有一个collectionView,其中有很多不同类型的单元格的行,但没有任何特定的模式(很难为每个单元格键入'if index path.row == 29'等等)。
我尝试了以下代码的变体,但它始终返回默认值(其他)。这里的代码 sn-p 有 3 种类型的单元格(GroupCell、MatchingButtonsCell 和 MatchingEventCell)。
我可以根据 Cell Class 使尺寸动态化吗?
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let cell = self.masterCollectionView.cellForItem(at: indexPath) as? GroupCell {
let cellHeight: CGFloat = (self.view.bounds.height * 0.55)
let cellWidth: CGFloat = self.masterCollectionView.frame.width
return CGSize(width: cellWidth, height: cellHeight)
} else if let cell = self.masterCollectionView.cellForItem(at: indexPath) as? MatchingButtonsCell {
let cellHeight: CGFloat = 60
let cellWidth: CGFloat = self.masterCollectionView.frame.width
return CGSize(width: cellWidth, height: cellHeight)
} else if let cell = self.masterCollectionView.cellForItem(at: indexPath) as? MatchingEventCell {
let cellHeight: CGFloat = 500
let cellWidth: CGFloat = self.masterCollectionView.frame.width
return CGSize(width: cellWidth, height: cellHeight)
} else {
let cellHeight: CGFloat = 10
let cellWidth: CGFloat = self.masterCollectionView.frame.width
return CGSize(width: cellWidth, height: cellHeight)
}
}
提前谢谢你!
【问题讨论】:
标签: swift xcode uicollectionview uicollectionviewcell uicollectionviewlayout