gpt4 book ai didi

ios - 如何使用 indexPathsForSelectedItems 在 Collection View 中获取选定项

转载 作者:IT王子 更新时间:2023-10-29 05:14:57 34 4
gpt4 key购买 nike

我有一个照片的 collectionView,想将被点赞的照片传递给 detailViewControler。

采集数据来自:

 var timeLineData:NSMutableArray = NSMutableArray ()

我想使用 prepare for segue 方法。

我的问题是如何从被点击的单元格中获取正确的 indexPath?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue == "goToZoom" {
let zoomVC : PhotoZoomViewController = segue.destinationViewController as PhotoZoomViewController
let cell = sender as UserPostsCell

let indexPath = self.collectionView!.indexPathForCell(cell)
let userPost = self.timeLineData.objectAtIndex(indexPath!.row) as PFObject
zoomVC.post = userPost

}
}

最佳答案

prepareForSegue:sender: 中的 sender 参数将是单元格,如果你从单元格连接 segue。在这种情况下,您可以从单元格中获取 indexPath,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showZoomController" {
let zoomVC = segue.destinationViewController as PhotoZoomViewController
let cell = sender as UICollectionViewCell
let indexPath = self.collectionView!.indexPathForCell(cell)
let userPost = self.timeLineData.objectAtIndex(indexPath.row) as PFObject
zoomVC.post = userPost
}
}

关于ios - 如何使用 indexPathsForSelectedItems 在 Collection View 中获取选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314852/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com