Различные типы ячеек в одном collectionView

У меня есть 4 разных collectionViews в одном контроллере, и в первом collectionView я хочу, чтобы отображались 3 разных ячейки. В приведенном ниже коде приложение не падает, но в первом indexPath.item из 0 загружается только случай 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 - withReuseIdentifier "cellId" в collectionViewController

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 - withReuseIdentifier "cellId4" в collectionViewController

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

    }

}

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)
}
Другие вопросы по тегам