CollectionView 设置 头标题

注意:
1. CollectionView 的头标题位置 和 他的 滚动 方向 相关
2.一定要 注册 头标题
3. 在初始化Layout 的时候 设置 flowLayout.headerReferenceSize = CGSizeMake(JKScreenWidth, 23);
不然不显示

不多说,直接 上 代码

创建:

pragma mark - 组织成员 CollectionView

  • (void)setupMemberCollectionView{

    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //根据自己的需求设置宽高
    [flowLayout setItemSize:CGSizeMake(40, 40)];
    //水平滑动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //内边距,列、行间距
    flowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0);
    flowLayout.minimumInteritemSpacing = 12;
    flowLayout.minimumLineSpacing = 12;
    flowLayout.headerReferenceSize = CGSizeMake(JKScreenWidth, 23);

    UICollectionView * memberCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, JKScreenWidth, 100) collectionViewLayout:flowLayout];
    memberCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:memberCollectionView];

    [memberCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {

      make.top.equalTo(_managerCollectionView.mas_bottom).mas_equalTo(20);
      make.left.equalTo(_managerCollectionView);
      make.right.mas_equalTo(0);
      make.height.mas_equalTo(64);
    

    }];
    memberCollectionView.delegate = self;
    memberCollectionView.dataSource = self;
    memberCollectionView.bounces = NO;
    memberCollectionView.scrollEnabled = NO;
    memberCollectionView.showsHorizontalScrollIndicator = NO;
    memberCollectionView.backgroundColor = [UIColor whiteColor];
    self.memberCollectionView = memberCollectionView;

}

注册:

pragma mark - 注册

  • (void)registerCollectionViewCell{

    [_managerCollectionView registerClass:[RSMeGroupDetailsCollectionViewCell class] forCellWithReuseIdentifier:croupDetailsIdentifier];

    [_managerCollectionView registerClass:[RSMeGroupDetailsCollectionReusableView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:reusableViewIdentifier];

[_memberCollectionView registerClass:[RSMeGroupDetailsCollectionViewCell class] forCellWithReuseIdentifier:croupDetailsIdentifier];

[_memberCollectionView registerClass:[RSMeGroupDetailsCollectionReusableView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:reusableViewIdentifier];

}

代理: 在代理 方法 设置 头视图 或者 脚视图

  • (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

     RSMeGroupDetailsCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reusableViewIdentifier forIndexPath:indexPath];
    

    if (collectionView == _managerCollectionView) {
    headerView.nameLabel.text = @"管理员";
    }else if(collectionView == _memberCollectionView){

        headerView.nameLabel.text = @"组织成员";
    

    }

    return headerView;

}

你可能感兴趣的:(CollectionView 设置 头标题)