通过segue将CollectionView的选定单元格索引发送到viewController

时间:2015-07-06 07:51:00

标签: ios swift uicollectionview segue

我有一个CollectionView。我想通过segue将选定的单元格索引发送到ViewController。为此我做了:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    println("Cell \(indexPath.row) selected!")

    self.performSegueWithIdentifier("categoryItems", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "menuItems"{
        var menuController = segue.destinationViewController as! MenuViewController
        if let indexPath = collectionView.indexPathForCell(sender as! UICollectionViewCell) {
            menuController.selectedCell = "\(indexPath.row)"
        }
    }
}

但它没有发送任何东西。问题是什么?我只想在ViewController中打印它,其中:

var selectedCell : String?

myLabel.text = selectedCell

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您已使用标识符

进行检查
if segue.identifier == "menuItems"

并且您正在使用此标识符执行segue:

self.performSegueWithIdentifier("categoryItems", sender: self)

menuItems不能神奇地变成categoryItems,你自己要做点什么:)