title: Segue from a UICollectionView in Swift
body: <p>This question probably has an extremely simple answer, but I can't get my head around it. Nor can I find a particularly useful answer that works anywhere online.</p>
  
  <p>I simply can't get a segue working from a UICollectionViewCell. I'm possibly coming at it from the wrong angle, as I assumed it worked in a very similar way to a table view segue. Working on that premise, if I was using a table view, it would look something like this:</p>
  
  <pre><code>if segue.identifier == "TripsToPastTripDetailsSegue" {
          let newViewController = segue.destinationViewController as! pastTripDetailViewController
          let selectedRow: NSManagedObject = tripsList[tableView.indexPathForSelectedRow!.row] as NSManagedObject
          newViewController.passedTrip = selectedRow as! Trips   
      }
  </code></pre>
  
  <p>So, with the table view I would be passing a single object (from Core Data) over to the another view controller. Simple. But I just can't translate that over to a collection view.</p>
  
  <p>I have been attempting something along these lines:</p>
  
  <pre><code>if segue.identifier == "pastTripCollectionToSavedLocationSegue" {
          let newViewController = segue.destinationViewController as! pastTripDetailViewController
          let selectedRow: NSManagedObject = locationsList[collectionView.indexPathForCell(pastLocationImageCollectionViewCell)] as! NSManagedObject
          newViewController.passedTrip = selectedRow as! Trips   
      }
  </code></pre>
  
  <p>But this gives me an error of <strong>Cannot find member 'indexPathForCell'</strong>, so I'm obviously doing something very wrong. This is one of many errors I've had whilst playing around with the code.</p>
  
  <p>Can someone point me in the right direction?</p>
  
  <p>EDIT:</p>
  
  <p>As requested by @Laffen, the UICollectionView appears in the following locations in the code:</p>
  
  <pre><code>class pastTripDetailViewController: UIViewController,  UICollectionViewDataSource, UICollectionViewDelegate, MKMapViewDelegate {
  </code></pre>
  
  <p>As an IBOutlet:</p>
  
  <pre><code>@IBOutlet weak var collectionView: UICollectionView!
  </code></pre>
  
  <p>Data source and delegate in ViewDidLoad:</p>
  
  <pre><code>self.collectionView.dataSource = self
  self.collectionView.delegate = self
  </code></pre>
  
  <p>And here's the rest:</p>
  
  <pre><code>func collectionView(collectionView: UICollectionView, numberOfSectionsInCollectionView indexPath: Int) -&gt; Int {
      return 1
  }
  
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return self.locationsList.count
  }
  
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
  
      let location = self.locationsList[indexPath.row]
      let cell: pastLocationImageCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as! pastLocationImageCollectionViewCell
      cell.locationImage.image = UIImage(data: location.image!)
  
      return cell
  }
  
  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
      performSegueWithIdentifier("pastTripCollectionToSavedLocationSegue", sender: self)
  }
  </code></pre>