indexPathForSelectedRow的collectionView形式是什么?

时间:2016-08-31 02:29:23

标签: swift xcode uicollectionview nsindexpath

我正在尝试创建一个变量“indexx”,它将被segued到我的下一个tableController。 indexx将保存我的collectionView中所选单元格的indexPath,但是,我不确定要使用哪个属性。我对tableController属性非常熟悉,所以我很难找到这段代码的成功解决方案。你碰巧知道我的错误吗?

快速说明 - foodcv是我的collectionView,而numberr是第二个目标控制器中的变量。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destViewController = segue.destination as? foodtable {
        let indexx = self.foodcv.indexPath(for: foodcell)
        destViewController.numberr = indexx
    }
}


let numberr : IndexPath = []

2 个答案:

答案 0 :(得分:1)

也许已经太晚了,但是你走了:

Swift 4

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destViewController = segue.destination as? foodtable {
        let indexPaths : NSArray = foodcv.indexPathsForSelectedItems! as NSArray
        let indexx : IndexPath = indexPaths[0] as! IndexPath
        destViewController.numberr = indexx[selectedindexPath.row]
    }
} 

答案 1 :(得分:-1)

使用此修改后的实现

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destViewController = segue.destination as? foodtable {
        let indexx = self.foodcv.indexPathForCell(sender as! UICollectionViewCell)
        destViewController.numberr = indexx
    }
}