【问题标题】:UICollectionview has unwanted space between sectionsUICollectionview 在部分之间有不需要的空间
【发布时间】:2020-08-20 04:48:49
【问题描述】:

我为我的 horizo​​ntal UICollectionView 设置了部分。我希望每个部分第二行中的单元格项目是第一行中项目的两倍宽度。这是我设置的:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0 {
        return 2
    }
    return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { //1.7, 2.7
    if indexPath.row == 0 {
        return CGSize(width: collectionView.frame.size.width/2,
        height: collectionView.frame.size.height/2)
    }
    return CGSize(width: collectionView.frame.size.width,
                  height: collectionView.frame.size.height/2)

}

但这是最终结果:

我怎样才能摆脱较小尺寸物品中的空间并很好地布局它们?

注意:图片上的数字代表section和row(0 1 -> Section 0, Row 1)

这就是我想要实现的目标:

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewlayout uicollectionviewflowlayout


    【解决方案1】:

    首先:如果您使用故事板,请确保将集合视图的约束正确添加到其超级视图。

    第二:添加collection view cell的约束。

    第三:从故事板的大小检查器,将集合视图的估计大小更改为无。

    第四:在你的viewcontroller中添加UICollectionViewDelegateFlowLayout

    第五:添加以下代码:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: (collectionView.frame.size.width - 10)/2,
            height: (collectionView.frame.size.height - 10)/2) // your desired height
        }
        return CGSize(width: (collectionView.frame.size.width - 10),
                      height: (collectionView.frame.size.height - 10)/2)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
                return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 0.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                return 5
    }
    

    【讨论】:

    • 感谢您的详细解答。这是正确设置布局大小,但宽度较大的单元格位于第 2 节的顶部。所以它看起来像这样:第 0 节,索引 0:小,第 0 节,索引 1:小,第 1 节,索引 0:大.. 我需要的是:第 0 节,索引 0:小,第 0 节,索引 1:大,第 1 节,索引 0:小。所以顶部的所有项目都必须很小,底部的所有项目都应该很大。我说清楚了吗?
    • 嗯,这很令人困惑,首先,你有多少个部分?根据您的问题,有 2 个部分,那么您如何获得第 2 部分。只有第 0 部分和第 1 部分。
    • 无论如何,我的解决方案是根据您的第二张图片。尝试根据您的部分在 sizeForItemAt 方法中进行调试。您可以查看此 [链接] [developer.apple.com/documentation/uikit/uicollectionview/…
    • 是的,你是对的,这是第 0 节和第 1 节。但你的答案中的错误是;在第 1 节中将第一行设置为大。在我的第二张图片中,大的是第 1 行。所有部分中的第 0 行始终是小图像。
    • 仅供参考:这是我应用您的代码后得到的:ibb.co/0fPJcZV 黑点是模拟器的右边缘,请忽略..@Asif Newaz
    【解决方案2】:

    将 indexPath.row 替换为 indexPath.section。

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { //1.7, 2.7
        if indexPath.section == 0 {
            return CGSize(width: collectionView.frame.size.width/2,
            height: collectionView.frame.size.height/2)
        }
        return CGSize(width: collectionView.frame.size.width,
                      height: collectionView.frame.size.height/2)
    
    }
    
    
    
    
    

    【讨论】:

    • 这会发生:ibb.co/0fPJcZV 但在这里我们希望同一部分的第 1 行具有不同的大小