iOS

UICollectionView添加SectionHeader

2020-09-01  本文已影响0人  songjk

注册view

[_collView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];

代理方法

            //组头高度
        -(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
            
            return CGsizeMake(SCREEN_WIDTH, 100);
        }
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
            UILabel *label = [[UILabel alloc]init];
            label.text = @"hello";
            for (UIView *view in header.subviews) {
                [view removeFromSuperview];
            } 
            [header addSubview:label];
            return header;
            
        } else {
            return nil;
        }
        
    }
上一篇 下一篇

猜你喜欢

热点阅读