[英]Swift 2.1 - How to pass index row of collection view cell to another view controller
I'm trying to send indexPath.row of selected cell in collection view to a destination controller (detail view) and I've done the following so far 我正在尝试将集合视图中选定单元格的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 is a type of NSIndexPath so I've tried to convert to Int but I get Cannot assign value of type '(UnsafePointer<Int8>,Int32) -> UnsafeMutablePointer<Int8>' to type 'Int'
at runtime indexPath.row是NSIndexPath的一种类型,因此我尝试转换为Int,但
Cannot assign value of type '(UnsafePointer<Int8>,Int32) -> UnsafeMutablePointer<Int8>' to type 'Int'
在运行时Cannot assign value of type '(UnsafePointer<Int8>,Int32) -> UnsafeMutablePointer<Int8>' to type 'Int'
In the destination view controller I've initialized var index = 0
to receive the indexPath.row value 在目标视图控制器中,我已初始化
var index = 0
以接收indexPath.row值
Any idea why I get this error at runtime? 知道为什么我在运行时会收到此错误吗?
这是一个collectionView,所以我相信您应该使用indexpath.item而不是.row
You have the following line in didSelectItemAtIndexPath
: 在
didSelectItemAtIndexPath
包含以下行:
var index: Int = indexPath.row
This declares index
as being local to this function only. 这将
index
声明为仅对该函数本地。 Then in prepareForSegue
you have: 然后在
prepareForSegue
您可以:
detailVC!.index = index
Since you're not getting a compilation error, index
must also be defined somewhere else. 由于没有编译错误,因此
index
也必须在其他位置定义。 It's this somewhere else variable that didSelectItemAtIndexPath
should be setting. didSelectItemAtIndexPath
应该在其他地方设置此变量。 It is probably just 可能只是
index = indexPath.row
Move the following out of the function and make it a property. 将以下内容移出该函数并使其成为属性。
var index: Int = indexPath.row
In prepareForSegue you have the following: 在prepareForSegue中,您具有以下内容:
detailVC!.index = index
The variable 'index' isn't declared in the class or locally, so what you get is a function named 'index' which is defined as: 变量“ index”未在类中或本地声明,因此您得到的是一个名为“ index”的函数,其定义为:
func index(_: UnsafePointer<Int8>, _: Int32) -> UnsafeMutablePointer<Int8>
If you make 'index' a property, it will be used instead of the function of the same name. 如果将“索引”设置为属性,则将使用它代替相同名称的函数。
Another solution could be: 另一个解决方案可能是:
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 = collectionView.indexPathsForSelectedItems()?.first?.item
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.