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

Jack Ryder
Jack Ryder
7,286 Points

UICollectionView

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
Jack Ryder
7,286 Points

sorted 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
Patrick Donahue
9,523 Points

Drag a UICollectionView on the the view.

Or

Or drag a UICollectionViewController onto the story board and add a segue to it.

I 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
Jack Ryder
7,286 Points

any 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
Patrick Donahue
9,523 Points

Do you have an error message?

Did you set the UICollectionView and an IBOutlet?

Jack Ryder
Jack Ryder
7,286 Points

Yes 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
Patrick Donahue
9,523 Points

Did you add the class to the view in the storyboard screen?

Jack Ryder
Jack Ryder
7,286 Points

Which class do I need to add?

Patrick Donahue
Patrick Donahue
9,523 Points

The ViewController that is going to have the code that runs the CollectionView.

Jack Ryder
Jack Ryder
7,286 Points

I've got a UICollectionViewController in my storyboard

Patrick Donahue
Patrick Donahue
9,523 Points

I understand that but you need to set the class in interface builder.

Jack Ryder
Jack Ryder
7,286 Points

Yes I've set the class to my custom class HomeViewController which is shown above, any ideas Sam Soffes

thanks for your help!

Is 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
Jack Ryder
7,286 Points

I got around this by using a CollectionViewObject so that i could initialise it in my own way without setting it as the rootView

Found a cheeky way around having to set it as RootView.

http://stackoverflow.com/questions/22553009/ios-scrollview-inside-uitablecellview

Jack Ryder
Jack Ryder
7,286 Points

No error message. What would the IBOutlet be linked to?

You 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?