如何使用 indexPathsForSelectedItems 在集合视图中获取选定项目 [英] How to get a selected item in collection view using indexPathsForSelectedItems
本文介绍了如何使用 indexPathsForSelectedItems 在集合视图中获取选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个照片的collectionView,想将被点击的照片传递给detailViewControler.
I have a collectionView of photos and want to pass the photo who was cliked to a detailViewControler.
采集数据来自:
var timeLineData:NSMutableArray = NSMutableArray ()
我想使用 prepare for segue 方法.
I would like to use the prepare for segue method.
我的问题是如何从被点击的单元格中获取好的 indexPath ?
My problem is how to get the good indexPath from the cell who was clicked ?
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,
The sender argument in prepareForSegue:sender: will be the cell if you connected the segue from the cell. In that case you can get the indexPath from the cell,
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
}
}
这篇关于如何使用 indexPathsForSelectedItems 在集合视图中获取选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文