0
votes

I have a custom UICollection View Cell. One of that cell's property's is cellImageView. When the user selects a cell, I want to pass the image from that view (cellImageView.image) to the next one. Here's what I've tried:

if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell)
{
    if segue.identifier == "show"
    {
        var segue = segue.destinationViewController as! ViewPhoto
        segue.imageURL = URLToPass

    }
}

but quickly realized that's the wrong route to take. What's the best way to go about doing this? Thanks in advance.

1
look perfectly fine to me, what do you think is wrong about it? - Icaro
looks find to me as well, except for that variable name segue, which is ambiguous. - Shali Liu
After going this route, there is no way to get the cellImageView.image - Jimmy McDermott
Also, my custom class for the cell is called GalleryClass, not sure if that's important - Jimmy McDermott

1 Answers

0
votes
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{ 
  if let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as? GalleryClass
  {
    let imageToPass = cell.cellImageView.image
    self.performSegueWithIdentifier(mySegue, sender: imageToPass)
  }
}

And in your prepareForSegue function:

if segue.identifier == "show"
{
  var segue = segue.destinationViewController as! ViewPhoto
  // segue.imageURL = URLToPass
  segue.image = sender as! UIImage
}

This way you're keeping code which contains CollectionView stuff in the own delegate method, which is easier to maintain if you have to change it in the future. And you only pass data in your prepareForSegue function which you actually need in there.