首页 > 解决方案 > 在 3 个不同的 CollectionView 中选择特定的单元格以执行对特定视图控制器的 Segue

问题描述

我在一个视图控制器中有 3 个单独的集合视图,每个集合视图中有 3 个项目。我希望用户从每个 collectionView 中选择 1 个单元格,并根据选择的内容选择特定的视图控制器。

我知道我目前正在使用“ If, Else 语句可能涉及一些逻辑。我还使用了 didSelectItemAt 对每个 collectionView 的单元格使用 performSegueWithIdentifier 但是,当用户只在一个 collectionView 中选择 1 个单元格时总是会发生这种情况我没有弄清楚我要去哪里错了。

这是我一直在使用的代码;

class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {

  @IBOutlet weak var collectionViewRed: UICollectionView!

  @IBOutlet weak var collectionViewBlue: UICollectionView!

  @IBOutlet weak var collectionViewGreen: UICollectionView!

  var redColors = ["Red", "Infrared", "Crimson"]

  var blueColors = ["Blue", "Light Blue", " Navy Blue"]

  var greenColors = ["Green", "Lime Green", "Forest Green"]


func collectionView(_ collectionView: UICollectionView, didSelectItemAt: indexPath: IndexPath) {

 if collectionView == self.collectionViewRed {

   let cellA = collectionViewRed.cellForItem(at: indexPath) as!     
   CollectionViewCell

   self.performSegue(withIdentifier: "ShowNextView", sender nil)

 } else if collectionViewBlue == self.collectionViewBlue {

   let cellB = collectionViewBlue.cellForItem(at: indexPath) as! 
   CollectionViewCell

   self.performSegue(withIdentifier: "ShowNextView", sender: nil)

 } else if collectionView == self.collectionViewGreen {

   let cellC = collectionViewGreen.cellForItem(at: indexPath) as!   
   CollectionViewCell

}


}

当用户单击 Red CollectionView 中的“Red”时,它执行到 Next ViewController 的 segue,而不是当用户从“redCollectionView”中选择“Red”,然后从“blueCollectionView”中选择“Blue”,最后是“ Green”从“greenCollectionView”执行到特定 ViewController 的 segue。

我应该使用什么代码来允许用户从每个 collectionView 中选择 1 个项目然后执行 segue?

标签: swift

解决方案


您应该有 3 个布尔存储所有集合视图的选定单元格的状态

将这些初始化为 false

var isFirstCollectionViewSelected = false
var isSecondCollectionViewSelected = false
var isThirdCollectionViewSelected = false

// You can also use this for the states, in that case you would not even need the above variables 
var firstSelectedIndex: Int?  

所以你的didSelectItemAt会看起来像

func collectionView(_ collectionView: UICollectionView, didSelectItemAt: indexPath: IndexPath) {

 if collectionView == self.collectionViewRed {
   isFirstCollectionViewSelected = true

   self.firstSelectedIndex = indexPath.row

   // Add logic here when this cell is selected  

 } else if collectionView == self.collectionViewBlue {
   isSecondCollectionViewSelected = true

   // Add logic here when this cell is selected  

 } else if collectionView == self.collectionViewGreen {
   isThirdCollectionViewSelected = true

   // Add logic here when this cell is selected 

 }

 performSegueIfAllAreSelected()
}

performSegueIfAllAreSelected()会在哪里

func performSegueIfAllAreSelected() {

  if isFirstCollectionViewSelected, isSecondCollectionViewSelected, isThirdCollectionViewSelected { 
     if self.firstSelectedIndex == 1 {  
      self.performSegue(withIdentifier: "ShowNextView", sender: nil)
     } else {
     //
     }

  }

}

推荐阅读