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

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!