4

我在一个控制器中有 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 中的 withReuseIdentifier "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

    }

}
4

1 回答 1

5

你的意思是先过滤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)
}
于 2017-06-25T19:45:03.347 回答