多次调用UICollectionView viewForSupplementaryElementOfKind

时间:2017-05-05 10:49:33

标签: ios objective-c uicollectionview

我的UIViewController上附有UICollectionView。问题是每次向上滚动集合视图时都会调用viewForSupplementaryElementOfKind。有没有办法停止拨打viewForSupplementaryElementOfKind一次以上?

2 个答案:

答案 0 :(得分:1)

调用viewForSupplementaryElementOfKind取决于节的数量和节头/页脚的可用性。只要在视图中显示有页眉或页脚的部分,就会调用viewForSupplementaryElementOfKind

答案 1 :(得分:0)

最后,我想出了一个像魅力一样的解决方案...... 我在创建标签时第一次设置标签值,并在每次进入循环时检查viewForSupplementaryElementOfKind中是否设置了标签值。如果已经设置,那么它将不再重新设计标签

- (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;
}