我試圖將集合視圖中選定單元格的indexPath.row發送到目標控制器(詳細視圖),我已經完成了以下操作遠Swift 2.1 - 如何將收集視圖單元格的索引行傳遞給另一個視圖控制器
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是,所以我已經試過轉換爲int類型NSIndexPath,但我在運行時
得到Cannot assign value of type '(UnsafePointer<Int8>,Int32) -> UnsafeMutablePointer<Int8>' to type 'Int'
在我初始化var index = 0
接收indexPath目標視圖控制器.row值
任何想法,爲什麼我在運行時遇到這個錯誤?
你在哪裏得到這個錯誤?在哪一行?在'prepareForSegue'你從哪裏得到'index'?它看起來像一個屬性變量,但在'didSelectItemAtIndexPath'它是一個局部變量。你的意思是將索引屬性設置爲'didSelectItemAtIndexPath'中的indexPath.row –