0

I am using Alamofire for fetching the images from the webservice. I am showing those image in a custom cell in UICollectionView with the following code

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let reuseIdentifier = "cell"
    // get a reference to our storyboard cell
    let cell : ImageCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellCollection", forIndexPath: indexPath) as! ImageCollectionCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.backgroundColor = UIColor.yellowColor() // make cell more visible in our example project
    let imageGalleryURLString = self.MutableArray.objectAtIndex(indexPath.row) .valueForKey("thumbnail") as! String
    let imageURL = NSURL(string:imageGalleryURLString)
    cell.imageviewCollectionCell.sd_setImageWithURL(imageURL)
    return cell
}

But the application crashes at "cell.imageviewCollectionCell.sd_setImageWithURL(imageURL)" showing "fatal error: unexpectedly found nil while unwrapping an Optional value"

I have went through all possible links and i haven't been satisfied yet

Ashley Rodrigues
  • 153
  • 2
  • 10
  • I think your imageURL returns nil ..check that...and dont force unwrap use if let or guard – Bhavin Bhadani Jun 30 '16 at 06:39
  • @EICaptainv2.0 Thanks! but i am not getting the imageURL as nill. Please be clear where to use let or guard in this code?? – Ashley Rodrigues Jun 30 '16 at 06:42
  • Try stepping through in a debugger and check that your `cell` is actually something (it should be, but just to be sure). Next, is the `imageviewCollectionCell` something? and is it properly wired/connected from the `@IBOutlet` to the actual imageView? It doesn't seem so based on the error message :) – pbodsk Jun 30 '16 at 06:48
  • @pbodsk I did -> po cell.imageviewCollectionCell and got fatal error: unexpectedly found nil while unwrapping an Optional value – Ashley Rodrigues Jun 30 '16 at 06:51
  • yep, there's your problem :) Seems your `imageviewCollectionCell` outlet isn't properly wired to an `UIImageView` (I guess?) and therefore it crashes when you try to access it. Check that you have a valid `@IBOutlet` for your `imageviewCollectionCell` and verify that the outlet is connected in your `xib` file (hope that makes sense :)) – pbodsk Jun 30 '16 at 06:54
  • @pbodsk i have created an UIImageView outlet in the cell dear – Ashley Rodrigues Jun 30 '16 at 06:59
  • @AshleyRodrigues Okay then you have to debug where you getting nil. – Ashish Kakkad Jun 30 '16 at 07:00
  • @AshleyRodrigues OK, you have create an UIImageView outlet in your `ImageCollectionCell` class, but is it connected to an UIImageView in your xib file? If you have just made the `imageviewCollectionCell` and not connected it, then it is nil when you try to access it. – pbodsk Jun 30 '16 at 07:05
  • @pbodsk i have created and connected too dear – Ashley Rodrigues Jun 30 '16 at 07:06
  • @AshleyRodrigues OK...hmm, I'm running out of ideas, but maybe there is something in here that can help you: http://stackoverflow.com/questions/25165195/why-is-uicollectionviewcells-outlet-nil (in short, dont call `registerClass` if you are using a storyboard and make absolutely sure that your `identifier` in the storyboard and when you try to create the cell is the same). – pbodsk Jun 30 '16 at 07:12

1 Answers1

5

Sorry I had registered the custom UICollectionViewCell in viewDidLoad method using

collectionView!.registerClass(customCell.self, forCellWithReuseIdentifier: "Cell")

And i am using storyboard . After removing this code everything works fine.

Ashley Rodrigues
  • 153
  • 2
  • 10