我想创建一个像图片上那样的收藏视图
ios - 如何为UICollectionViewCell设置不同的填充大小-LMLPHP
到目前为止我的控制器有这个代码

class MainCollController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate  {

let cellId = "collectionCellId"

override func viewDidLoad() {
    super.viewDidLoad()


    let flowLayout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
    collectionView.register(Cell.self, forCellWithReuseIdentifier: cellId)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.backgroundColor = UIColor.grayBg

    self.view.addSubview(collectionView)

    _ = collectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    let width = UIScreen.main.bounds.width
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: width / 2, height: width / 2)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    collectionView.collectionViewLayout = layout

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width / 2 , height: 280)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 20
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! Cell
    cell.backgroundColor = UIColor.cyan
    cell.cellIndex = indexPath.row

    return cell
}

}

这个是给牢房的。我想检查indexPath.row是偶数还是加法,并根据它设置不同的约束。
class Cell: UICollectionViewCell {

var cellIndex: Int? {
    didSet {
        if cellIndex! % 2 == 0 {
            _ = productView.anchor(topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 20, bottomConstant: 0, rightConstant: 8.5, widthConstant: 0, heightConstant: 264)
        } else {
            _ = productView.anchor(topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 11.5, bottomConstant: 0, rightConstant: 19.5, widthConstant: 0, heightConstant: 264)
        }
    }
}

let productView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.white
    view.layer.borderColor = UIColor(red:0.867, green:0.867, blue:0.867, alpha:1.000).cgColor
    view.layer.borderWidth = 0.5
    return view
}()

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(productView)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

当我打开我的应用程序,它看起来确实是我所需要的,但当我开始向下滚动和向上滚动时,一切都变得复杂。
ios - 如何为UICollectionViewCell设置不同的填充大小-LMLPHP
你能帮我整理一下吗。谢谢!

最佳答案

您需要实现这两个函数,才能使collectionView像您所希望的那样:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0,15,0,15)
}

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

为了你的目的,你可以自由发挥自己的价值观。

10-08 12:01