0

So seems -addSubview twice add UILabel to the UICollectionReusableView

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        if (reusableview==nil) {
            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        }

        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        label.text=[NSString stringWithFormat:@"Recipe Group #%i", indexPath.section + 1];
        [reusableview addSubview:label];
        return reusableview;
    }
    return nil;
}

This code does not work:

if (reusableview==nil) {
   reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}

So the reusable view is never nill.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

1 Answers1

0

If you want to customize a UICollectionReusableView, you need to subclass it the same way you would for a UICollectionViewCell. Else you're basically adding a label everytime your reusableview is reused (on scroll or reloadData for ex).

I made a post about that a while ago : UICollectionView adding image to a cell

Just do the same thing for your reusableview and let me know if it solves your problem.

Community
  • 1
  • 1
Kujey
  • 1,122
  • 6
  • 17