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
}
}