swift - 如何使用 push segues 处理两个 Collection View

标签 swift uicollectionview segue uicollectionviewcell uistoryboardsegue

我有两个 UICollectionView

@IBOutlet weak var collectionView1: UICollectionView!

@IBOutlet weak var collectionview2: UICollectionView!

我正在分别使用函数获取每个 Collection View 的 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]
    }
}

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

我有错误 在

let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController

这是第一个 if 语句条件值,我被困在这里

请帮助我,如何处理对每个 Collection View 的单元格单击执行两个 segue。

最佳答案

使用UICollectionView协议(protocol)中的方法

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

关于swift - 如何使用 push segues 处理两个 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813103/

相关文章:

ios - 以编程方式快速更改 segue 标识符名称

ios - 登录成功后如何呈现不同的ViewController

ios - UITableView - reloadRows 方法创建奇怪的滚动错误

swift - 欧拉计划 #35 - 圆素数

swift - 如何在单击时突出显示 UIButton 的背景和 "unhighlight"呢?

ios - UICollectionView 和补充 View (标题)

ios - 如何执行 Action 而不触发segue?

ios - 将数据库连接用作静态是一种好习惯吗?

ios - 使用 UIEdgeInsets 将 scrollView 定位在 NavController 下面并让它在 SWIFT 中滚动到后面

ios - UICollectionView 动画单元格大小更改会导致不良行为