【发布时间】:2015-07-06 07:50:59
【问题描述】:
我有一个 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
我做错了什么?
【问题讨论】:
-
您为 performSegueWithIdentifier 传递的发件人是 View controller not selected Cell。另一种方法是创建变量来保存选定的单元格索引。传递该值后将其设置为零。或者使用方法 indexPathsForSelectedItems 来获取选定的索引。
标签: ios swift uicollectionview segue