如何使用 push segues 处理两个集合视图

how to handle two collection view with push segues

我有两个 UICollectionView

@IBOutlet weak var collectionView1: UICollectionView!

@IBOutlet weak var collectionview2: UICollectionView!

我正在为每个集合视图分别获取 indexPath 和函数。

func getIndexPathForSelectedCell() -> NSIndexPath?
{

    var indexPath:NSIndexPath?

    if collectionview1.indexPathsForSelectedItems()!.count > 0 {
        indexPath = collectionview1.indexPathsForSelectedItems()![0]
    }
    return indexPath
}

func getIndexPathForSelectedCell2() -> NSIndexPath?
{

    var indexPath2:NSIndexPath?

    if collectionView2.indexPathsForSelectedItems()!.count > 0 {
        indexPath2 = collectionView2.indexPathsForSelectedItems()![0]
    }
    return indexPath2
}

我正在为单元格触摸执行 segue,如下所示。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if let indexPath = getIndexPathForSelectedCell()
    {

        let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController

        DealsdetailViewController.Dealsdata = Dealsdata[indexPath.row]
    }
    else if let indexPath2 = getIndexPathForSelectedCell2()
    {

        let ContainerviewController = segue.destinationViewController as! ContainerViewController

        ContainerviewController.BTdata = BTdata[indexPath2.row]
    }
}

如果我在第一个集合视图中单击一个单元格,那么当我在第二个集合视图中单击一个单元格时,segue 会正确执行

我出错了 在

let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController

这是第一个 if 语句条件值,我卡在这里了

请帮助我,如何处理在每个集合视图上单击单元格时执行两个 segue。

使用 UICollectionView 协议中的方法

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = cellForItemAtIndexPath(indexPath)!
    if collectionView == self.collectionView1 {
        self.performSegueWithIdentifier("segue1", sender: cell)
    } else if collectionView == self.collectionView2 {
        self.performSegueWithIdentifier("segue2", sender: cell)
    }
}

func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
    if segue.identifer == "segue1" {
      let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
      // Your sender is cell. You have indexPath of them and can get his identity in dataSource.
      //detailVC.name = ...
      //detailVC.surname = ...
    } else if segue.identifier == "segue2" {
      //...
    }
}