2017-03-09 18 views
0

我有一个导航选项卡栏,每个选项卡包含一个数组的图片,请参阅下图。我的问题是。为什么这个枚举只能看到顶级案例“男士”,它不会转向女性,艺术和保存。我试图删除案例男士,那么它只能看到案例“女人”,无论是在顶部,这只是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   
     } 
     } 

Setting 情况下 Tab Bar

+0

什么是分配后'tab'的价值? – Alexander

+0

对不起亚历山大,我不明白你的意思。 – Cookingcodes

+0

'guard let tab = ...'和'switch tab'之前'tab变量的值是什么? – Alexander

回答

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.sectionindexPath.row

+0

已起草类似答案。这应该是解决方案。 –

+0

这仍然是一样的,女性艺术和保存的照片仍然没有出现。 :( – Cookingcodes

+0

你可以打印'indexPath'并看看有什么值? – manishg