【问题标题】:different types of cells in one collectionView一个 collectionView 中的不同类型的单元格
【发布时间】:2017-06-25 18:50:26
【问题描述】:

我在一个控制器中有 4 个不同的 collectionView,在第一个 collectionView 中我想显示 3 个不同的单元格。在下面的代码中,应用程序不会崩溃,但在第一个 indexPath.item of 0 中,只有 case 0: ("cellId") 加载。它不加载其他 2 个案例。提前感谢您的帮助!

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if indexPath.item == 0 {
             switch indexPath.item {
            case 0:
                return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
            default:
                return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId4", for: indexPath)
            }

        } else if indexPath.item == 1 {
            return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath)

        } else if indexPath.item == 2 {
            return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath)

        } else {

        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
        return myCell

        }
    }

// 2 个 collectionView 单元格 - 每个单元格中只有 1 个部分
// 单元格 1 - 在 collectionViewController 中使用 ReuseIdentifier "cellId"

class TravelGuideHomeCellForStats: BaseHomeCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    let cellId = "cellId"


    override func setupViews() {
        super.setupViews()

        collectionView.register(BaseTravelGuideHomeCell.self, forCellWithReuseIdentifier: cellId)
    }

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

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

        return cell
    }

}

// 单元格 2 - 在 collectionViewController 中使用 ReuseIdentifier "cellId4"

class TravelGuideCommentsCell: BaseCommentsCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    let cellId = "cellId"

    override func setupViews() {
        super.setupViews()

        collectionView.register(BaseTravelGuideCommentsCell.self, forCellWithReuseIdentifier: cellId)
    }

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

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

        return cell

    }

}

【问题讨论】:

  • 您确定在该集合视图中有 3 个部分吗?
  • 我将代码更新为只有 2 个 switch case,并添加了 collectionView 单元子类。谢谢

标签: swift uicollectionview switch-statement uicollectionviewcell indexpath


【解决方案1】:

您的意思是先过滤section,而不是视图控制器中的item

即像这样:

if indexPath.section == 0 {
    switch indexPath.item {
        ...
    }
} else if indexPath.section == 1 {
    return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath)

} else if indexPath.section == 2 {
    return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath)

} else {

    let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
    return myCell
}

虽然我会这样做作为嵌套开关:

switch indexPath.section { 
case 0:
    switch indexPath.item {
    ...
    } 
case 1:
    return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath)
case 2:
    return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath)
default:
    return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
}

【讨论】:

  • 我不得不重新组织我的基本单元类,但这有效 - 谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多