prepareForSegue collectionView indexPath с использованием swift
Я не нашел хорошего примера, чтобы делать то, что я хочу, используя swift, поэтому я позволю себе задать вопрос.
Я использую collectionView для отображения объектов PFObject, и я хочу отправить отображаемые данные ячейки на второй контроллер, используя prepareForSegue.
На данный момент, я изо всех сил пытаюсь заставить эту часть кода работать:
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'
пожалуйста, дайте мне знать, если я использую неправильный метод, или если вам нужны дополнительные данные, чтобы получить соответствующий обзор проблемы.
ОТВЕТ
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
}
}
}