如何在Swift中将NSIndexPath转换为Int?

5

我需要获取我的UICollectionView中单元格的索引。我知道如何获取它,但问题是当我将这个NSIndexpath转换为Int时。以下是我的代码。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let cell = sender as! UICollectionViewCell
    let index = self.collectionView!.indexPathForCell(cell)!

    let vc : VideosViewController = segue.destinationViewController as! VideosViewController
    vc.id_category =  self.result_category[index.row as Int]["id"].intvalue
}

当用户选择一个单元格时,您是否触发了segue? - Fred Faust
1个回答

9

您不需要将index.row转换为Int,因为它已经是一个Int类型了。请看苹果的声明:

var row: Int { get }

在类NSIndexPath中。
只需执行vc.id_category = self.result_category[index.row]["id"].intValue。只要小心数组中的越界索引即可。
希望这可以帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接