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

时间:2016-04-23 16:02:17

标签: swift uicollectionview segue uicollectionviewcell uistoryboardsegue

我有两个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
}

我正在按照以下步骤进行细胞触摸。

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。

1 个答案:

答案 0 :(得分:2)

使用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" {
      //...
    }
}