美文网首页iOS
UICollectionView添加SectionHeader

UICollectionView添加SectionHeader

作者: songjk | 来源:发表于2020-09-01 09:58 被阅读0次

    注册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;
            }
            
        }

    相关文章

      网友评论

        本文标题:UICollectionView添加SectionHeader

        本文链接:https://www.haomeiwen.com/subject/rdwmsktx.html