gpt4 book ai didi

ios - swift 中的 gridLayout 问题

转载 作者:行者123 更新时间:2023-11-30 11:39:49 25 4
gpt4 key购买 nike

所以我有这些委托(delegate)方法来帮助我布局我的collectionView。我正在尝试创建某种网格布局,其中有 X numberOfSections 和 X numberOfItemsInSection。根据我现在所掌握的内容,它将所有权利排列在一起,并将它们间隔得有点远,以决定单元格进入下一行的位置。如何将所有单元格拟合为一行中的一个部分。

 //Asks the delegate for the size of the header view in the specified section.
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return .zero
}
//Asks the delegate for the size of the footer view in the specified section.
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return .zero
}
//Asks the delegate for the size of the specified item’s cell.
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.bounds.size.width - CGFloat(numberOfItemsInSection))/2.2, height: collectionView.bounds.size.height - 50)

}
//Asks the delegate for the margins to apply to content in the specified section.
//in short in controls the amount of space between the items above,left,right, and below
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
}
//Asks the delegate for the spacing between successive rows or columns of a section.
//controls the space in between rows and columns
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
//Asks the delegate for the spacing between successive items of a single row or column.
//controls the space between each cell
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}

我觉得它与 sizeForItemAt 有关,但我不知道如何更改它。

这就是目前的样子。

enter image description here

最佳答案

你是对的,它与 sizeForItemAt 方法有关,您应该使用 indexPath 参数,以便可以使用其属性 section定义每个部分中每个项目的尺寸。

请记住,项目之间有间距,因此在计算宽度和高度时需要考虑到这一点,因此您可以使用 minimumLineSpacingForSectionAtminimumInteritemSpacingForSectionAt 调整间距> 方法。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

if indexPath.section == 0 {
return CGSize(width: 200, height: 100)
}

return CGSize(width: (collectionView.bounds.size.width - CGFloat(numberOfItemsInSection))/2.2, height: collectionView.bounds.size.height - 50)

}

关于ios - swift 中的 gridLayout 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49374085/

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