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
Carter Randall
3,311 PointsUICollectionView not displaying Images?
I am creating a UICollectionView and am testing it with two photos in an array. The # number of items being returned from my array is correct (2) but my images are not showing?
Note:
-My imageView tag is set to 100
-the cell identifier is photo
-array is named photos
-Images are in xCode [ are they supposed to be in .xcassets?]
Here is my code: https://www.dropbox.com/s/ndc3luz870ucz7f/Screenshot%202014-12-10%2012.13.35.png?dl=0
Storyboard: https://www.dropbox.com/s/i0swxkr8ljdvrbk/Screenshot%202014-12-10%2012.14.41.png?dl=0
When I run the app in simulator: https://www.dropbox.com/s/vz0i6aso8s6nm50/Screenshot%202014-12-10%2012.15.18.png?dl=0
As always help is greatly appreciated :) Thank-you team treehouse.
3 Answers
Stone Preston
42,016 Pointsjust be sure you change the class of the collectionViewCell in storyboard to your new custom class. then you should be able to open the assistant editor and control+click and drag to connect your imageView as an outlet in your custom class. I would probably name the proeprty storeImageView.
then in cellForItem you would just set the imageView's image property:
cell.storeImageView.image = ...
Stone Preston
42,016 Pointslog the imageView and the image inside the cellForItem method and see what it shows:
NSLog("imageView: %@", storeImageView);
NSLog("image" %@", storeImageView.image);
Carter Randall
3,311 Pointshttps://www.dropbox.com/s/kztrvf82aehwce5/Screenshot%202014-12-10%2012.45.10.png?dl=0 Ok, all null that explains it. How do I fix that?
Stone Preston
42,016 PointsI think UICollectionView cell has an imageView by default, you may not have to add your own. try this:
cell.imageView.image = [UIImage imageNamed:[self.photos objectAtIndex:indexPath.row]];
additionally, if you wish to customize the cell, its probably best to create a custom subclass and hook up your UI elements of the cell as outlets to that subclass. Using tags works, but its generally a better practice to use a custom subclass
Carter Randall
3,311 PointsTurns out it does not have a imageView property: https://www.dropbox.com/s/q16oqgj206st2fv/Screenshot%202014-12-10%2013.51.13.png?dl=0 I will move the cell to its own custom class and see if that changes it.
Carter Randall
3,311 PointsCarter Randall
3,311 PointsTHANK YOU! It worked. What would you customize in the cell class? Should the outlet be of type weak or strong? Also I just want to say thank you to every mod on this forum. Particularity you, I see your answers everywhere and they have helped me a lot.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsgenerally the thing you use it for is just adding custom controls to a tableView/collection view cell. You dont have to add anything else if you dont need it. Outlets are generally weak properties.
and thanks, glad my answers have helped you out