Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Swift : Impossible to set imageView inside custom collection view

Hello

I have a custom collection view and i think it is well set up because I can change the backgroud color of the cells and it work well.

I have an ImageView in my custom cell, and i want to set the default image. When i add an image i have an error :

unexpectedly found nil while unwrapping an Optional value

What do i have to do ?

In viewDidLoad i have this :

  self.collectionView!.registerClass(UserPostsCollectionViewCell.self, forCellWithReuseIdentifier: "cell")

And my cell is setup like this

             override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:   NSIndexPath) -> UICollectionViewCell {
          let cell : UserPostsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UserPostsCollectionViewCell

          cell.backgroundColor = UIColor.blueColor()



          cell.postedImages.image = UIImage(named: "Avatar-1")

          return cell
         }

Of course i also created an outlet for the image in my UserPostsCollectionViewCell

12 Answers

may have found out whats going on. are you using a storyboard? if you are using a storyboard remove the following line from viewDidLoad:

self.collectionView!.registerClass(UserPostsCollectionViewCell.self, forCellWithReuseIdentifier: "cell")

that line will override what you have in your storyboard, so remove it

woo had same problem but this fixed it!

hmm I guess its possible your image view is nil for some reason. try logging the image view to the console and see what you get:

  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:   NSIndexPath) -> UICollectionViewCell {
          let cell : UserPostsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UserPostsCollectionViewCell

          cell.backgroundColor = UIColor.blueColor()



          println(cell.postedImages);

          return cell
         }

its possible you missed a superclass method call somewhere and your image view is not being set up right

it print me "nil" for each cell

can you post the code for your custom cell class

yep :

import UIKit

 class UserPostsCollectionViewCell: UICollectionViewCell {   

@IBOutlet var postedImages: UIImageView!

}

try removing the outlet and adding it again.

Same thing, "nil". It's crazy.

if you want to zip your project up on dropbox/github ill take a look.

Thank you Stone, first i just want to show you my collectionViewController, did i miss something ?

 import UIKit



 class UserPostsCollectionViewController: UICollectionViewController {



   override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:   NSIndexPath) -> UICollectionViewCell {
    let cell : UserPostsCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UserPostsCell

    cell.backgroundColor = UIColor.blueColor()


   // cell.postedImages.image = UIImage(named: "Avatar-1")
    println(cell.postedImages)
    return cell
    }


  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)


  }

     override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
     //#warning Incomplete method implementation -- Return the number of sections
     return 1
   }


     override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) ->  Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return self.timeLineData.count
    }



      override func viewDidLoad() {
    super.viewDidLoad()


}

     override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

   }



   }

Note that i changed the name of my UICollectionViewCell to UserPostsCell by creating a new one. This is not a mistake.

print the value of the cell to the console, we need to verify that is in fact of type UserPostCell:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:   NSIndexPath) -> UICollectionViewCell {
    let cell : UserPostsCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UserPostsCell

    cell.backgroundColor = UIColor.blueColor()


   // cell.postedImages.image = UIImage(named: "Avatar-1")
    println(cell.postedImages)
    println(cell)
    return cell

Yes it is !

   <swrd.UserPostsCell: 0x7fc78e3754c0; baseClass = UICollectionViewCell; frame = (0 0; 160 160); layer =      <CALayer: 0x7fc78e373630>>

can you post what was logged

see my edited post

hmm. strange. try adding a text view or something to the cell, connect it as an outlet, and try settings that and see if it lets you do that. your code looks like it should be working

Same thing with label...maybe it's a bug because i installed the OS X Yosemite GM Candidate 2 but not Xcode 6.1 GM seed 2 ?

I m going to install the last xcode build and see....

dont do that yet, i think we can figure it out

Yeeeeeaaaaahhhhh ! That works ! So many Thanks Stone.

woo! glad you got it working.