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!
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

Jack Ryder
7,286 PointsUICollectionView
I've been working through the photo bombers app and was wondering what steps I would have to take to implement a UICollectionView in the storyboard?
Thanks!
13 Answers

Jack Ryder
7,286 Pointssorted the problem, i was making changes in the init method but never called it! I missed this code from the delegate:
self.window.rootViewController = [[HomeViewController alloc] init];

Patrick Donahue
9,523 PointsDrag a UICollectionView on the the view.
Or
Or drag a UICollectionViewController onto the story board and add a segue to it.

Stephen Whitfield
16,771 PointsI haven't messed with it yet, but there's a CollectionView object you can drag onto the storyboard in the Objects Library. I imagine the manipulation of the cells to be the same, if not slightly different than that of a TableView.

Jack Ryder
7,286 Pointsany ideas why this code won't work? i have a UICollectionView in the storyboard with the cell identifier named photo.
#import "HomeViewController.h"
#import <Parse/Parse.h>
@interface HomeViewController ()
@end
@implementation HomeViewController
-(instancetype) init {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(106.0, 106.0);
layout.minimumInteritemSpacing = 100.0;
layout.minimumLineSpacing = 1.0;
return (self = [super initWithCollectionViewLayout:layout]);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"photo"];
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
NSLog(@"Current User: %@", currentUser.username);
}
else {
[self performSegueWithIdentifier:@"showLogin" sender:self];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showLogin"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 20;
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"photo";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightGrayColor];
return cell;
}
@end

Patrick Donahue
9,523 PointsDo you have an error message?
Did you set the UICollectionView and an IBOutlet?

Jack Ryder
7,286 PointsYes I have taken a number of other iOS courses, I don't think an IBOutlet is required in this case. There's no action that needs to be executed specifically

Patrick Donahue
9,523 PointsDid you add the class to the view in the storyboard screen?

Jack Ryder
7,286 PointsWhich class do I need to add?

Patrick Donahue
9,523 PointsThe ViewController that is going to have the code that runs the CollectionView.

Jack Ryder
7,286 PointsI've got a UICollectionViewController in my storyboard

Patrick Donahue
9,523 PointsI understand that but you need to set the class in interface builder.

Jack Ryder
7,286 PointsYes I've set the class to my custom class HomeViewController which is shown above, any ideas Sam Soffes
thanks for your help!

chriscameron
15,397 PointsIs there any way to this without having to set the CollectionView as rootView in AppDelegate? I ask because I'm having this problem. I can get it all working fine, but I don't want the CollectionView to be the first View.
Thanks for any help.

Jack Ryder
7,286 PointsI got around this by using a CollectionViewObject so that i could initialise it in my own way without setting it as the rootView

chriscameron
15,397 PointsFound a cheeky way around having to set it as RootView.
http://stackoverflow.com/questions/22553009/ios-scrollview-inside-uitablecellview

Jack Ryder
7,286 PointsNo error message. What would the IBOutlet be linked to?

Stephen Whitfield
16,771 PointsYou can't just drag an object to your storyboard and expect Xcode to know that you want to do something with it. You have to connect it to your code. Have you taken other iOS courses?