Swift 一个collectionView中不同类型的单元格
我在一个控制器中有4个不同的collectionView,在第一个collectionView中,我想显示3个不同的单元格。在下面的代码中,应用程序不会崩溃,但在0的第一个indexath.item中,仅加载案例0:(“cellId”)。它不加载其他2个案例。提前感谢您的帮助Swift 一个collectionView中不同类型的单元格,swift,uicollectionview,switch-statement,uicollectionviewcell,indexpath,Swift,Uicollectionview,Switch Statement,Uicollectionviewcell,Indexpath,我在一个控制器中有4个不同的collectionView,在第一个collectionView中,我想显示3个不同的单元格。在下面的代码中,应用程序不会崩溃,但在0的第一个indexath.item中,仅加载案例0:(“cellId”)。它不加载其他2个案例。提前感谢您的帮助 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollect
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-在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
}
}
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
}
}
//单元格2-在collectionViewController中使用ReuseIdentifier“cellId4”
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
}
}
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
}
}
您的意思是要先按
部分
进行过滤,而不是按视图控制器中的项
进行过滤吗
i、 e.像这样:
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)
}
您确定在该集合视图中有3个部分吗?我更新了代码,使其只有2个切换案例,并添加了collectionView单元格子类。thanksI不得不重新组织我的基本单元类,但这起了作用-谢谢!