arunprasath sivasamy
arunprasath sivasamy

Reputation: 228

UICollectionView viewForSupplementaryElementOfKind called multiple times

I have a UIViewController with a UICollectionView attached on it. The problem is that viewForSupplementaryElementOfKind is called every time when I scroll up the collection view. Is there any way to stop calling viewForSupplementaryElementOfKind more than one time?

Upvotes: 0

Views: 2020

Answers (2)

Ashutosh Dave
Ashutosh Dave

Reputation: 380

The calling of viewForSupplementaryElementOfKind depends on the number of the sections and availability of Section header/footer. The viewForSupplementaryElementOfKind is going to get called whenever a section that has a header or footer gets displayed in the view

Upvotes: 1

arunprasath sivasamy
arunprasath sivasamy

Reputation: 228

Finally I came up with a solution that works like a charm... I set a tag value for label for the first time when I create it and check whether the tag value is set or not in viewForSupplementaryElementOfKind every time when I enter the loop. If it is set already then it will not redesign the label again

- (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 viewWithTag:101] == nil)
      {
         [[reusableview viewWithTag:101] removeFromSuperview];
         pointsAmountLabel = [[UILabel alloc] init];
         pointsAmountLabel.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 20);
         pointsAmountLabel.text = @"0";
         pointsAmountLabel.tag = 101;
         pointsAmountLabel.font = [UIFont boldSystemFontOfSize:16];
         [reusableview addSubview:pointsAmountLabel];
     }
    return reusableview;
   }
return nil;
}

Upvotes: 0

Related Questions