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

标签 ios swift uicollectionview nsindexpath

我有一个照片的 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/

相关文章:

ios - UICollectionViewLayout layoutAttributesForElementsInRect 和 layoutAttributesForItemAtIndexPath

ios - Objective-C读取十六进制代码并输出表情符号

iOS 架构 vs 有效架构和设置的影响

swift - 从 NSInputStream 读取 IntegerType

swift - 如何从函数中的结构访问该值? swift

ios - UICollectionView 找不到成员

iphone - 我们应该使用哪些 iOS 设备来进行合理的功能和性能测试?

ios - 如何加入圆角线

ios - 将 PFUser 属性分配给 UILabel

ios - 如果有多个 UICollectionView,则在一个 UICollectionView 中选择了哪个项目