在3个不同的CollectionView中选择特定的单元以对特定的View Controller执行Segue

时间:2019-04-07 21:38:05

标签: swift

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

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

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

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中的“红色”时,它将执行到Next ViewController的选择,而不是用户从“ redCollectionView”中选择“红色”,然后从“ blueCollectionView”中选择“蓝色”,并且然后最后从“ greenCollectionView”中选择“绿色”,以对特定的ViewController执行设置。

我应该改用什么代码,以便允许用户从每个collectionView中选择一项,然后执行segue?

1 个答案:

答案 0 :(得分:0)

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

将这些初始化为false

# Insert after df generated, before plot call
library(forcats)
df <- df %>% 
  mutate(Var1 = as_factor(Var1),
         Var1 = fct_lump_min(Var1, min = 501, w = Freq, other_level = "≥5"))

所以您的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()