我有一个导航标签栏,每个标签包含一个数组图片,见下图。我的问题是。为什么enum只看到顶级案例“男装”,它不会转向女性,艺术和拯救。我试图删除案例男装,然后它只能看到案例“女性”,无论是什么在顶部,这只是cellForItemAtIndexPath可以看到的东西。一切都已经连接起来我试图让它们一个接一个地看看它们是否正在工作而且它们都在工作。但它只能看到最重要的东西。我不明白这里缺少什么,
class StreamDetailController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{
private enum Tabs: Int {
case mens
case women
case arts
case saved
}
var menImage = [Men]()
var artsImage = [Arts]()
var womenImage = [Women]()
var savedImage = [SavedPhotos]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let tab = Tabs(rawValue: section) else {
assertionFailure()
return section
}
switch tab {
case .mens:
return menImage.count
case .women:
return womenImage.count
case .arts:
return artsImage.count
case .saved:
return savedImage.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let tab = Tabs(rawValue: indexPath.section) else {
assertionFailure()
return UICollectionViewCell()
}
switch tab {
case .mens:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let men = menImage[indexPath.item]
cell.boxImage.image = men.image
return cell
case .women:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let women = womenImage[indexPath.item]
cell.boxImage.image = women.image
return cell
case .arts:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let art = artsImage[indexPath.item]
cell.boxImage.image = art.image
return cell
case .saved:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let save = savedImage[indexPath.item]
cell.boxImage.image = save.image
return cell
}
}
答案 0 :(得分:2)
问题是您使用section
而非row
来识别该行。很可能你只有一个部分和多个行。这就是为什么该部分始终为零并指向枚举的第一个元素的原因。您应该将代码更改为以下内容:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let tab = Tabs(rawValue: indexPath.row) else {
assertionFailure()
return UICollectionViewCell()
}
switch tab {
case .mens:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let men = menImage[indexPath.item]
cell.boxImage.image = men.image
return cell
case .women:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let women = womenImage[indexPath.item]
cell.boxImage.image = women.image
return cell
case .arts:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let art = artsImage[indexPath.item]
cell.boxImage.image = art.image
return cell
case .saved:
let cell = extraCells.dequeueReusableCell(withReuseIdentifier: "Cells", for: indexPath) as! ExtraViewCell
let save = savedImage[indexPath.item]
cell.boxImage.image = save.image
return cell
}
}
我已将indexPath.section
替换为indexPath.row