title: prepareForSegue collectionView indexPath using swift
body: <p>I don't find any good example to do what I want using swift so I allow myself to ask the question.</p>
  
  <p>Im using a collectionView to display PFObjects and I want to send the displayed cell data to a second controller using prepareForSegue.</p>
  
  <p>At this point, Im struggling to make this part of the code works:</p>
  
  <pre><code>override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          if(segue.identifier == "goto_answerquestion"){
              var indexpath : NSIndexPath = self.collectionView.indexPathsForSelectedItems()
          }
      }
  </code></pre>
  
  <p>this line:</p>
  
  <pre><code>var indexpath : NSIndexPath = self.collectionView.indexPathsForSelectedItems()
  </code></pre>
  
  <p>triggers the following error: </p>
  
  <pre><code>(UICollectionView, numberOfItemsInSection: Int)-&gt; Int does not have a member named 'indexPathsForSelectedItems'
  </code></pre>
  
  <p>please let me know if Im using the wrong method, or if you need additional data to have the appropriate overview of the problem.</p>
  
  <p><strong>ANSWER</strong></p>
  
  <pre><code>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
              }
          }
      }
  </code></pre>