use*_*224 3 switch-statement uicollectionview uicollectionviewcell swift indexpath
我在一个控制器中有4个不同的collectionViews,在第一个collectionView中,我想显示3个不同的单元格。在下面的代码中,该应用程序不会崩溃,但在第一个indexPath.item为0的情况下,仅加载0:(“ cellId”)。它不会加载其他两种情况。在此先感谢您的帮助!
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
}
}
Run Code Online (Sandbox Code Playgroud)
// 2个collectionView单元格-每个单元格中只有一个部分
// //单元格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
}
}
Run Code Online (Sandbox Code Playgroud)
//单元格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
}
}
Run Code Online (Sandbox Code Playgroud)
您是要首先过滤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
}
Run Code Online (Sandbox Code Playgroud)
虽然我可以将其作为嵌套开关来完成:
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3989 次 |
| 最近记录: |