如何设置自定义collectionView单元格框架?

时间:2018-09-21 11:12:54

标签: ios swift uicollectionview

我要先UICollectionViewCell到整个屏幕宽度,再UICOllectionViewCell是2 * 2。

为此,我尝试了以下代码。

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == 0 {
        let cell : FirstCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FirstCell", for: indexPath) as! FirstCell

        cell.layoutSubviews()
        return cell
    }
    else {
        let cell : SecondCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SecondCell", for: indexPath) as! SecondCell

        cell.layoutSubviews()
        return cell
    }
}

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

    if indexPath.item == 0 {
        let width   = (UIScreen.main.bounds.size.width - 16)
        return CGSize(width: width, height: 200)
    }
    else {
        let width   = (UIScreen.main.bounds.size.width - 24) / 2
        let height  = (UIScreen.main.bounds.size.width * 200) / 320
        return CGSize(width: width, height: height)
    }
}

但是输出如下。

enter image description here

要求:第一个绿色单元格具有全宽,第二个黄色单元格不是来自中心,而是来自左侧,而第3,第4个单元格则是2 * 2

任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

要实现此目的,您应该在“收藏夹”视图中选择3个部分。

  1. 第1-1节
  2. 第2-1节的单元格(宽度应小于收集单元格可用宽度的一半)
  3. 第3部分-您想要的单元格数量

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

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

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        switch indexPath.section {
        case 0:
            return CGSize(width: UIScreen.main.bounds.width - 20, height: 200)
        case 1:
            return CGSize(width: (UIScreen.main.bounds.width - 30) / 2, height: 200)
        default:
            return CGSize(width: (UIScreen.main.bounds.width - 30) / 2, height: 200)
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncell", for: indexPath)
        cell.backgroundColor = .red
        return cell
    }
}

您的视图将如下所示:

enter image description here