prepareForSegue collectionView indexPath使用Swift [英] prepareForSegue collectionView indexPath using swift

查看:98
本文介绍了prepareForSegue collectionView indexPath使用Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何很好的示例使用swift来完成我想做的事情,所以我允许自己问这个问题。

I don't find any good example to do what I want using swift so I allow myself to ask the question.

我使用collectionView来显示PFObjects和我想使用prepareForSegue将显示的单元格数据发送到第二个控制器。

Im using a collectionView to display PFObjects and I want to send the displayed cell data to a second controller using prepareForSegue.

在这一点上,我正在努力使这部分代码起作用:

At this point, Im struggling to make this part of the code works:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "goto_answerquestion"){
            var indexpath : NSIndexPath = self.collectionView.indexPathsForSelectedItems()
        }
    }

此行:

var indexpath : NSIndexPath = self.collectionView.indexPathsForSelectedItems()

触发以下错误:

(UICollectionView, numberOfItemsInSection: Int)-> Int does not have a member named 'indexPathsForSelectedItems'

请让我知道我是否使用了错误的方法,或如果您需要其他数据以对问题进行适当的概述。

please let me know if Im using the wrong method, or if you need additional data to have the appropriate overview of the problem.

ANSWER

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "segue_identifier"){
            // check for / catch all visible cell(s)
            for item in self.collectionView!.visibleCells() as [UICollectionViewCell] {
                var indexpath : NSIndexPath = self.collectionView.indexPathForCell(item as CollectionViewCell)!
                var cell : CollectionViewCell = self.collectionView!.cellForItemAtIndexPath(indexpath) as CollectionViewCell

                // Grab related PFObject
                var objectData:PFObject = self.questionData.objectAtIndex(indexpath.row) as PFObject

                // Pass PFObject to second ViewController
                let theDestination = (segue.destinationViewController as answerPageViewController)
                theDestination.questionObject = objectData
            }
        }
    }


推荐答案

如果你只是试图只找到被轻击的单元格的索引路径,而不是有多个,可以在prepareForSegue方法中做到这一点:

If you just are trying to just find the index path of the cell tapped, and not have multiple, you could do this in your prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPath = collectionView.indexPathForCell(sender as UICollectionViewCell)
    // do what you need now with the indexPath
}

发件人在这种情况下是您所点击的单元格,因此您只需要将其强制转换为UICollectionViewCell(或者,如果您创建了一个自定义单元格子类)。

sender in this situation is the cell you tapped, so you just need to cast it to UICollectionViewCell (or a custom cell subclass, if you made one).

UPDATE

Swift 1.2为此引入了 as!来替换 as ,因此为了安全起见,您可以在 prepareForSegue 中使用多个绑定进行尝试:

UPDATE:
Swift 1.2 introduced as! to replace as for this purpose, so to keep with safety, you can try this inside prepareForSegue using multiple bindings:

if let cell = sender as? UICollectionViewCell, indexPath = collectionView.indexPathForCell(cell) {
    // use indexPath
}

这篇关于prepareForSegue collectionView indexPath使用Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆