【发布时间】:2016-03-18 03:55:55
【问题描述】:
我正在尝试将集合视图中选定单元格的 indexPath.row 发送到目标控制器(详细视图),到目前为止我已经完成了以下操作
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let recipeCell: Recipe!
recipeCell = recipe[indexPath.row]
var index: Int = indexPath.row
performSegueWithIdentifier("RecipeDetailVC", sender: recipeCell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "RecipeDetailVC" {
let detailVC = segue.destinationViewController as? RecipeDetailVC
if let recipeCell = sender as? Recipe {
detailVC!.recipe = recipeCell
detailVC!.index = index
}
}
}
indexPath.row 是 NSIndexPath 的一种类型,所以我尝试转换为 Int,但在运行时得到 Cannot assign value of type '(UnsafePointer<Int8>,Int32) -> UnsafeMutablePointer<Int8>' to type 'Int'
在目标视图控制器中我初始化了var index = 0
接收 indexPath.row 值
知道为什么我会在运行时收到此错误吗?
【问题讨论】:
-
你在哪里得到这个错误?在哪一行?在
prepareForSegue中,您从哪里获得index?它看起来像一个属性变量,但在didSelectItemAtIndexPath中它是一个局部变量。您的意思是在didSelectItemAtIndexPath中将 index 属性设置为 indexPath.row