Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.