Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 2.1-如何将集合视图单元格的索引行传递给另一个视图控制器_Ios_Swift - Fatal编程技术网

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

Ios Swift 2.1-如何将集合视图单元格的索引行传递给另一个视图控制器,ios,swift,Ios,Swift,我正在尝试将集合视图中选定单元格的indexath.row发送到目标控制器(详细视图),目前为止,我已经完成了以下工作 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let recipeCell: Recipe! recipeCell = recipe[indexPath.row] var index: I

我正在尝试将集合视图中选定单元格的indexath.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,但在运行时,
无法将类型为“(UnsafePointer,Int32)->UnsafeMutablePointer”的值分配给类型“Int”

在目标视图控制器中,我已初始化
var index=0
接收indexPath.row值


你知道我为什么在运行时出现这个错误吗?

这是一个集合视图,所以我认为你应该使用indexpath.item not.row

这是一个集合视图,所以我认为你应该使用indexpath.item not.row

你在
didSelectItemAtIndexPath
中有以下行:

var index: Int = indexPath.row
这将
索引声明为仅此函数的本地索引。然后在
prepareForSegue
中,您有:

detailVC!.index = index
由于没有得到编译错误,
索引也必须在其他地方定义。
didSelectItemAtIndexPath
应该设置的是这个别处变量。可能只是

index = indexPath.row

didSelectItemAtIndexPath
中有以下行:

var index: Int = indexPath.row
这将
索引声明为仅此函数的本地索引。然后在
prepareForSegue
中,您有:

detailVC!.index = index
由于没有得到编译错误,
索引也必须在其他地方定义。
didSelectItemAtIndexPath
应该设置的是这个别处变量。可能只是

index = indexPath.row

将以下内容移出函数并使其成为属性

var index: Int = indexPath.row
在prepareForSegue中,您有以下内容:

detailVC!.index = index
变量'index'未在类中或本地声明,因此您得到的是一个名为'index'的函数,其定义如下:

func index(_: UnsafePointer<Int8>, _: Int32) -> UnsafeMutablePointer<Int8>
func索引(u:UnsafePointer,u:Int32)->UnsafeMutablePointer

如果将“index”设置为属性,则将使用它而不是同名函数。

将下列内容移出函数,并将其设置为属性

var index: Int = indexPath.row
在prepareForSegue中,您有以下内容:

detailVC!.index = index
变量'index'未在类中或本地声明,因此您得到的是一个名为'index'的函数,其定义如下:

func index(_: UnsafePointer<Int8>, _: Int32) -> UnsafeMutablePointer<Int8>
func索引(u:UnsafePointer,u:Int32)->UnsafeMutablePointer

如果将“索引”设置为属性,则将使用它而不是同名函数。

另一种解决方案可能是:

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
        }
    }
}

另一个解决办法可以是:

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
        }
    }
}

collectionView本身可以处理行,但默认情况下indexath.item是Int类型,所以对我来说应该是更好的选择。我的问题是
var指数
,我本应该在全球范围内建立它。Thanke.collectionView本身可以处理行,但默认情况下indexpath.item是一种Int类型,所以对我来说它应该是一个更好的选择。我的问题是
var指数
,我本应该在全球范围内建立它。谢谢。你从哪里得到这个错误的?在哪一行?在
prepareForSegue
中,您从哪里获取
索引?它看起来像一个属性变量,但在
didSelectItemAtIndexPath
中它是一个局部变量。您的意思是在
didSelectItemAtIndexPath
中将index属性设置为indexPath.row吗?在哪一行?在
prepareForSegue
中,您从哪里获取
索引?它看起来像一个属性变量,但在
didSelectItemAtIndexPath
中它是一个局部变量。您的意思是在
didSelectItemAtIndexPath
中将index属性设置为indexPath.row吗?您是对的。我没有全局变量索引,我正在尝试访问此局部索引,该索引仅适用于
didSelectItemAtIndexPath
内部
prepareforsgue
您是对的。我没有全局变量索引,我正在尝试访问此局部索引,该索引仅适用于
didSelectItemAtIndexPath
内部
prepareforsgue