Pruthvi Hariharan
Pruthvi Hariharan

Reputation: 551

Implementing single cell selection to bring up a detail view in swift

Am working with Collection View, I displayed array of images in collection view, and now i need to display the selected image in another view in larger size. So i implemented as below.

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return logoImage.count
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

   let cell : PhotoCollection = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCells", forIndexPath: indexPath) as PhotoCollection
    cell.imageView.image = logoImage[indexPath.row]
    return cell

}


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    var newCell :PhotoCollection = collectionView.cellForItemAtIndexPath(indexPath) as PhotoCollection
    var fullImageVw : FullImageClass = self.storyboard?.instantiateViewControllerWithIdentifier("FullImage") as FullImageClass
      fullImageVw.imageFull.image = newCell.imageView.image
    self.navigationController?.pushViewController(fullImageVw, animated: true)


}

But am getting the error unexpectedly found nil while unwrapping an Optional value in the line fullImageVw.imageFull.image = newCell.imageView.image. but newCell.imageView.image has a value still i don't know why am facing this error. Can anybody help me to solve this error.

Upvotes: 0

Views: 1074

Answers (2)

Bodlund
Bodlund

Reputation: 81

I encountered the same problem today. Since your view is created in storyboard but you are calling it programatically the view is not totally rendered at the time when you call fullImageVw.imageFull.image = newCell.imageView.image

A fix for you can be to run fullImageVw.view on the line before fullImageVw.imageFull.image = newCell.imageView.image in that way the imageView of fullImageVw.imageFull will not be nil and it will work fine Hope it helps

var newCell :PhotoCollection = collectionView.cellForItemAtIndexPath(indexPath) as PhotoCollection
var fullImageVw : FullImageClass = self.storyboard?.instantiateViewControllerWithIdentifier("FullImage") as FullImageClass
fullImageVw.view
fullImageVw.imageFull.image = newCell.imageView.image
self.navigationController?.pushViewController(fullImageVw, animated: true)

Upvotes: 0

Ian
Ian

Reputation: 12768

I usually create a segue in the storyboard that goes to the detail view when selected and then instead of using didSelectItemAtIndexPath i use prepareForSegue like so

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "DetailSegue"){
            let cell = sender as Cell
            var destination = segue.destinationViewController as DetailViewController

            destination.image = cell.imageView.image

        }
}

As far as your specific problem, I'm not really sure what's causing it to return nil, but I've done essentially what you're trying to do this way many times and it has worked so hopefully it works for you too.

Upvotes: 0

Related Questions