4

I have 4 different collectionViews in one controller, and in the first collectionView I want to have 3 different cells display. In the below code the app does not crash, but in the first indexPath.item of 0 only case 0: ("cellId") loads. It does not load the other 2 cases. Thanks in advance for any help!

    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 Cells - only 1 section in each cell
// cell 1 - withReuseIdentifier "cellId" in 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
    }

}

// cell 2 - withReuseIdentifier "cellId4" in 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

    }

}
user3708224
  • 1,229
  • 4
  • 19
  • 37

1 Answers1

5

Do you mean to be filtering by section first, instead of by item in your view controller?

i.e. like this:

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
}

Though I would have done this as nested switches:

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)
}
deanWombourne
  • 38,189
  • 13
  • 98
  • 110