CollectionView不会按预期显示3列

问题描述 投票:0回答:2

我在同一UICollectionView中有3个不同的UIViewController。他们工作正常。我的问题是样式,其中2个样式必须相同-意味着3列和一些间距。

这是我需要实现的目标:screenshot

这是我到目前为止所得到的:desired result

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if collectionView == gameCollectionView {
        let width = gameCollectionView.frame.size.width
        return CGSize(width: ((width / 2) - 7), height: 150)
    }
    else {
        let width = paymentCollectionView.bounds.width / 3 // < THIS IS What should give 3 columns 
        return CGSize(width: width, height: 70)
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

有人可以告诉我如何实现这一目标,并且最好解释一下它是如何工作的,因为我无法弄清楚

更新:根据要求-CollectionView的故事板Storyboard

ios swift uicollectionview uicollectionviewcell
2个回答
0
投票

尝试一下

CGSize(width:(((width-minimumInteritemSpacing)/ / [[numberofCellYouWant],height:150))您可以从collectionView.collectionViewLayout属性中获得minimumInteritemSpacing


0
投票
您需要更改此委托工作

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if collectionView == gameCollectionView { let width = gameCollectionView.frame.size.width return CGSize(width: ((width / 2) - 7), height: 150) } else { let width = (paymentCollectionView.bounds.width / 3) - 5 // give the more space you want return CGSize(width: width, height: 70) } }

© www.soinside.com 2019 - 2024. All rights reserved.