使用 UICollectionView
流委托(delegate),我试图完成此操作,但我仍然需要删除两个单元格区域中的巨大间距。
我的代码:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.row == 0 || indexPath.row == 5 {
return CGSize(width: (getWidth()/2) , height: (getWidth()/2))
}
return CGSize(width: (getWidth()/4) , height: (getWidth()/4))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
预期:
最佳答案
使用 plain UICollectionViewLayout
不可能做到这一点。从本质上讲,UICollectionViewLayout
是一种“line-braking-layout”,line-height 是其最高元素的高度。请看以下内容:
Apple docs on using UICollectionViewFlowLayout
但是,您可以通过子类化 UICollectionViewFlowLayout
来实现您想要的行为。这是有关如何操作的指南:
关于swift - UICollectionView 不同大小的单元格和项目计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53537544/