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

Adding a header to a collectionView object

I've tried to implement a header in my collectionView but get the error saying my 'UICollectionView data source is not set'

any ideas?

#import "ProfileViewController.h"
#import "PhotoCell.h"
#import <Parse/Parse.h>
#import "headerView.h"


@interface ProfileViewController ()

@end

@implementation ProfileViewController


- (void)viewDidLoad
{
    [super viewDidLoad];



    self.edgesForExtendedLayout = UIRectEdgeNone;

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    [layout setItemSize:CGSizeMake(306.0, 306.0)];
    [layout setMinimumInteritemSpacing:150.0];
    [layout setMinimumLineSpacing:50.0];



    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    [collectionView setDataSource:self];
    [collectionView setDelegate:self];

    [collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"jack"];
    [collectionView registerClass:[headerView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];



    [self.view addSubview:collectionView];


    [collectionView setBackgroundColor:[UIColor whiteColor]];



}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

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


    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"jack" forIndexPath:indexPath];


    cell.backgroundColor = [UIColor lightGrayColor];

    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {

        headerView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

    headerView.usernameLabel.text = @"hello";


        reusableview = headerView;
    }




    return reusableview;
}

@end

2 Answers

Sam Soffes
STAFF
Sam Soffes
Treehouse Guest Teacher

Jack,

I'm not quite sure what's going on, but I'd recommend switching from UIViewController to UICollectionViewController. Then you can remove your entire viewDidLoad method.

Jack Ryder
Jack Ryder
7,286 Points

I'll give that ago thanks Sam. Also I've been following your photo bombers course and wondered what steps would have to change when using parse as the source for the collectionView? Thanks

Sam Soffes
Sam Soffes
Treehouse Guest Teacher

Where the data source gets its data doesn't effect how the headers and such are displayed.

Jack Ryder
Jack Ryder
7,286 Points

Sam Soffes Yeah i know but was just wondering about that side topic if you could help!

Sam Soffes
Sam Soffes
Treehouse Guest Teacher

Oh misunderstood. Well, I'm not a Parse expert, but it should be very similar to how you do it in UITableView. UICollectionViewDataSource and UITableViewDataSource are very similar. Good luck!