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
jean-marie castellucci
4,954 PointsSwift : 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
Stone Preston
42,016 Pointsmay 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
Stone Preston
42,016 Pointshmm 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
jean-marie castellucci
4,954 Pointsit print me "nil" for each cell
Stone Preston
42,016 Pointscan you post the code for your custom cell class
jean-marie castellucci
4,954 Pointsyep :
import UIKit
class UserPostsCollectionViewCell: UICollectionViewCell {
@IBOutlet var postedImages: UIImageView!
}
Stone Preston
42,016 Pointstry removing the outlet and adding it again.
jean-marie castellucci
4,954 PointsSame thing, "nil". It's crazy.
Stone Preston
42,016 Pointsif you want to zip your project up on dropbox/github ill take a look.
jean-marie castellucci
4,954 PointsThank 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()
}
}
jean-marie castellucci
4,954 PointsNote that i changed the name of my UICollectionViewCell to UserPostsCell by creating a new one. This is not a mistake.
Stone Preston
42,016 Pointsprint 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
jean-marie castellucci
4,954 PointsYes it is !
<swrd.UserPostsCell: 0x7fc78e3754c0; baseClass = UICollectionViewCell; frame = (0 0; 160 160); layer = <CALayer: 0x7fc78e373630>>
Stone Preston
42,016 Pointscan you post what was logged
jean-marie castellucci
4,954 Pointssee my edited post
Stone Preston
42,016 Pointshmm. 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
jean-marie castellucci
4,954 PointsSame 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....
Stone Preston
42,016 Pointsdont do that yet, i think we can figure it out
jean-marie castellucci
4,954 PointsYeeeeeaaaaahhhhh ! That works ! So many Thanks Stone.
Stone Preston
42,016 Pointswoo! glad you got it working.
Jason Gong
4,537 PointsJason Gong
4,537 Pointswoo had same problem but this fixed it!