简体   繁体   English

如何使用推键处理两个集合视图

[英]how to handle two collection view with push segues

I'm having two UICollectionView 我有两个UICollectionView

@IBOutlet weak var collectionView1: UICollectionView!

@IBOutlet weak var collectionview2: UICollectionView!

i'm getting indexPath for each collection view separately with functions. 我分别为每个集合视图和函数获取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
}

I'm Performing segue for cell touch as follows. 我正在按以下步骤进行单元触摸的搜索。

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]
    }
}

if i click on a cell in first collection view segue performs correctly, when i click on a cell in second collection view 如果我在第一个集合视图中单击一个单元格,segue会正确执行,当我在第二个集合视图中单击一个单元格时

i got error in 我有错误

let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController

which is first if statement condition value, i'm stuck here 如果语句条件值是第一位,我就卡在这里

please help me, how to handle performing both segue on cell click on each collection view. 请帮助我,如何处理在单元格上执行两个segue单击每个集合视图。

Use the method from UICollectionView protocol 使用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" {
      //...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在视图控制器中为单个按钮创建两个序列? - How can I create two segues for a single button in a view controller? 如何在没有任何推送请求的情况下迅速从另一个View Controller访问方法? - How can I access a method from another View Controller in swift without any push segues? Swift 3-仅在视图控制器内部隔离集合视图的界面 - Swift 3 - Interface That Only Segues A Collection View Inside Of A View Controller 一个按钮可以有两个转场到两个不同的视图控制器/如何在没有转场的情况下编写视图控制器更改? - Can a button have a two segues to two different view controllers / how can i code a view controller change without a segue? 如何在SWIFT中的表格单元上分配两个序列? - How to assign Two segues on a table cell in SWIFT? 两个segue从一个视图控制器传递相同的数据 - Two segues passing the same data from one view controller 如何处理连续两次单击的UITabBarItem并消除串扰? - How Can I handle an UITabBarItem clicked twice consecutive and dismiss segues? 如何在两个容器视图中处理两个集合视图 - How to handle two collection views inside two container views 如何制作两行水平集合视图? - how to make two rows horizontal collection view? 如何通过indexPath添加两个集合视图单元格? - How to add two collection view cell by indexPath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM