我在一个控制器中有4个不同的collectionView,在第一个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 Cells - 每个单元格中只有1个部分
// cell 1 - collectionViewController中的withReuseIdentifier“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
}
}
// 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
}
}
答案 0 :(得分:4)
您的意思是先在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)
}