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

Rashii Henry
Rashii Henry
16,433 Points

adding ImageView to CollectionViewCell

I'm having a little trouble displaying an array of images at a specific index of each CollectionViewCell.

currently using : UILabel *itemLabel = (UILabel *)[cell viewWithTag:100]; itemLabel.text = [defaultBubblesArray objectAtIndex:indexPath.row]; //defaultBubblesArray is an array with strings.

&& the label works fine.

but when i try to do it with an UIImageView, button, or label, i either get an error or the images just don't show when the app loads.

1 Answer

Stone Preston
Stone Preston
42,016 Points

its best you use a custom collection view cell class. You wont have to mess with viewWIthTag if you do it with a custom cell class. Add a imageView to your collection view cell in storyboard then connect it up to your custom class's .h file. then just implement a few delegate methods like

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section


- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout    *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

and set the cells imageView property that you added in the custom class in the cellForItemAtIndexPath

Rashii Henry
Rashii Henry
16,433 Points

Even though I've already set the collection view as the data source and delegate?

i already tried creating a subclass of collectionViewCell class. After i did, i was getting some error in interface builder about the image view property. && after i delete the property connection outlet it will run again.

Stone Preston
Stone Preston
42,016 Points

Rashii Henry, you shouldnt set the collection view as the delegate you would set the delegate as your view controller. Have you tried using a UICollectionViewController?

Stone Preston
Stone Preston
42,016 Points

also be sure you set the cell identifier.