简体   繁体   English

Swift 2.1-如何将集合视图单元格的索引行传递给另一个视图控制器

[英]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.

相关问题 如何将集合视图单元格的indexPath传递给另一个视图控制器 - How to pass a collection view cell's indexPath to another view controller 将图像从集合视图控制器快速传递到另一个视图控制器 - swift pass images from collection view controller to another view controller 通过从Collection View Cell Swift通过segue将Image传递到另一个视图控制器 - Pass Image to another view controller through segue from Collection View Cell Swift Swift 2.1-如何通过collectionView单元格的索引路径行进行筛选 - Swift 2.1 - How to pass index path row of collectionView cell to segue 如何将数据数据从集合视图单元传递到视图控制器 - how to pass data data from collection view cell to view controller 如何在swift 3中的集合视图控制器中使用自定义单元格 - how to use custom cell in collection view controller in swift 3 如何在iOS Swift中设置特定索引的集合视图单元格? - How to set collection view cell of specific index in iOS Swift? 在集合视图中滚动后,单元格不显示-Swift 2.1 - Cell not display after scroll in collection view - swift 2.1 将数据从 tableview 单元格传递到 View Controller Swift 2.1 - Passing data from tableview cell to View Controller Swift 2.1 将数据从集合视图单元格按钮传递到另一个视图控制器(Swift) - Passing data from a collection view cell button to another view controller (Swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM