gpt4 book ai didi

swift - UICollectionView 不同大小的单元格和项目计数

转载 作者:行者123 更新时间:2023-11-28 05:49:07 24 4
gpt4 key购买 nike

使用 UICollectionView 流委托(delegate),我试图完成此操作,但我仍然需要删除两个单元格区域中的巨大间距。

enter image description here

我的代码:

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

预期:

enter image description here

最佳答案

使用 plain UICollectionViewLayout 不可能做到这一点。从本质上讲,UICollectionViewLayout 是一种“line-braking-layout”,line-height 是其最高元素的高度。请看以下内容:

Apple docs on using UICollectionViewFlowLayout

但是,您可以通过子类化 UICollectionViewFlowLayout 来实现您想要的行为。这是有关如何操作的指南:

Creating custom layouts

关于swift - UICollectionView 不同大小的单元格和项目计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53537544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com