Ostap Koziy
Ostap Koziy

Reputation: 167

UICollectionView header repeats every row

I'm subclassing UICollectionViewController.

UITableView header repeats after every row of cells that i have. I need the header to be "single" and always on the top of the screen (like the navigation bar).

I'm using the following code: my UICollectionViewController class:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [categoryArray count] / 2;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryCell" forIndexPath:indexPath];
    CategoryViewModel *category = [categoryArray objectAtIndex:(indexPath.section*2 + indexPath.row)];
    cell.name.text = category.name;
    cell.image.image = category.image;
    return cell;
}

-(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if(headerIndexPath == nil){
        headerIndexPath = indexPath;
    }
    CategoryScreenHeader *categoryScreenHeader = [collectionView dequeueReusableSupplementaryViewOfKind:
                                         UICollectionElementKindSectionHeader withReuseIdentifier:@"CategoryScreenHeader" forIndexPath:headerIndexPath];
    categoryScreenHeader.headerName.text = @"Some title";
    categoryScreenHeader.headerImage.image = [UIImage imageNamed:@"step-0.png"];
    return categoryScreenHeader;
}

Thank you.

Upvotes: 2

Views: 3434

Answers (2)

iago849
iago849

Reputation: 1854

I've found this brilliant and wonderful tutorial that works! http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using

Upvotes: 1

rdelmar
rdelmar

Reputation: 104092

UICollectionView doesn't have a table header like UITableView has (to the best of my knowledge), only section headers that scroll with the content.

If you want a view at the top of the screen that stays there, then you should add 2 subviews to your controller's view, one for your header and one for the collection view. Insert the collection view into that bottom subview instead of into the controller's main view. Delete the code you have in collectionView:viewForSupplementaryElementOfKind:atIndexPath: (unless you want section headers as well).

Upvotes: 0

Related Questions