将数据从CollectionView Delegate传递到另一个ViewController

时间:2016-04-06 07:32:48

标签: ios xcode swift uiviewcontroller uicollectionview

我是Swift的初学者。

我的CollectionView中有UIViewController,我需要在用户点击CollectionViewCell到另一个ViewController时传递数据。 我试图覆盖prepareForSegue,但我注意到它先调用didSelectItemAtIndexPath上的代码。

我的第一个ViewController代码:

import UIKit

class ViewController: UIViewController {

    ...

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController : UICollectionViewDataSource,UICollectionViewDelegate{

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collCell", forIndexPath: indexPath) as! MainCollectionViewCell

        ...
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        itemId = items[indexPath.item].id

        let distinationViewController = DistinationViewController()
        distinationViewController.itemId = itemId
    }   
}

我的Distination视图控制器代码

import UIKit

class DistinationViewController: UIViewController{

    var itemId : String!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print(itemId)
    }
}

itemIdDistinationViewController的值为nil

由于

3 个答案:

答案 0 :(得分:1)

您可以尝试通过以下代码更改视图:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        itemId = items[indexPath.item].id
        let distinationViewController = self.storyboard.instantiateViewControllerWithIdentifier("DestinationViewControllerStoryBoardID") as! DistinationViewController

        distinationViewController.itemId = itemId
self.navigationController.pushViewController(distinationViewController, animated: true)
}

答案 1 :(得分:1)

你需要做两件事:

  1. 将您的func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)更改为以下内容 之一:

        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    
            // Here you can check which Collection View is triggering the segue
            if collectionView == collectionView1 {
                self.performSegueWithIdentifier("fromFirstCollectionView", sender: nil);
            } else if collectionView == collectionView2 {
                // from other CollectionView
            }
        } 
    

    并且您的prepareForSegue方法将是这样的

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            // Even here you can check for segue.identifiers if you have something to differentiate
            let dvc = segue.destinationViewController as! DestinationViewController
            dvc.itemId = "Hello"
        }
    
  2. 从View Controller连接segue并为其指定identifier
  3. 根据您的评论,您有3个collectionView,所以我猜您需要3个带有3个不同标识符的segue。

    希望这有帮助!很高兴讨论你是否有更多问题。

答案 2 :(得分:0)

您可以将所有内容放入prepareForSegue中。像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier{
        switch identifier {
            case "distinationViewController":
                if let nextScene =  segue.destinationViewController as? DistinationViewController {
                    if let indexPath = self.collectionView.indexPathsForSelectedItems.lastItem {
                        nextScene.itemId=items[indexPath.item].id
                        nextScene.delegate = self
                }
            }
            default: break
        }
    }
}