CollectionView Header的使用

作者: David_Cap | 来源:发表于2015-08-05 17:14 被阅读31406次

CollectionView Header的添加和设置

建议先看CollectionView系列第一篇:http://www.jianshu.com/p/a1614404ae96

注册Header子类

这里不再赘述一些第一篇见过的东西,只介绍一下和添加 Cell不同的地方。

    //这里的HeaderCRView 是自定义的header类型
    [_muneView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Identifier];

自定义Header子类

自定义的话,header子类要继承自 UICollectionReusableView。然后在自行发挥。

要实现的代理方法

    //这个方法是返回 Header的大小 size
    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
    
    //这个也是最重要的方法 获取Header的 方法。
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        
        NSString *CellIdentifier = @"header";
        //从缓存中获取 Headercell
        HeaderCRView *cell = (HeaderCRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];
        return cell;
    }

当然 footer和header一样

相关文章

网友评论

  • 刘超_a594:headview有点击事件吗?
    类似这个方法
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  • muchDrinkHotWat:简单粗暴 很是关键
  • Oort:请问头部的点击事件怎么监听?
  • LD_左岸:Xib 描述的头视图也这样用吗
  • me_bingo:这是组头的header 有没有 整个collection view的header
    David_Cap:@me_bingo 应该 类似于TableView 有一个 headerView 的 property
  • 1cfd35193e59:为什么我添加了header后,进入页面会有延迟呢
  • 就是一个春天的花朵:老是崩在获取header的那个代理方法里面,报错indexpath为nil,有没有遇到过呢作者
  • 酒红色T恤:- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{


    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){

    self.headerViewForCollectionView = [collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:UICollectionViewHeaderID forIndexPath:indexPath];
    self.headerViewForCollectionView.frame = CGRectMake(0, 0, SCREEN_Width, 50);
    self.headerLabelForCollectionView = [[UILabel alloc]initWithFrame:CGRectMake((SCREEN_Width - 150)*0.5, 0, 150, 50)];
    self.headerLabelForCollectionView.text =@“请选择”;

    UIButton * deleteButton = [ UIButton buttonWithType:UIButtonTypeCustom];
    deleteButton.frame = CGRectMake(10, 10, 22, 22);
    [deleteButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(closeSelectCollectionView) forControlEvents:UIControlEventTouchUpInside];

    [self.headerViewForCollectionView addSubview:deleteButton];
    [self.headerViewForCollectionView addSubview:self.headerLabelForCollectionView];

    return self.headerViewForCollectionView;

    }else{
    return nil;
    }

    问题:我的collectionView的Header 不会重用,每次都加一次,叠在一起!不晓得哪里出了问题,麻烦看一下
    长风破浪初学者:怎么解决的.我也遇到这个问题了,望大神请教
    酒红色T恤:解决了
    d7fb243c2d17:@低碳环保_好好学习 您的问题解决了么,我现在也遇到了同样的问题。
  • 柠檬不争:用这种方法定义了3个section,为什么只有section==0,section==2时,有显示的标题.section==1的title为空?
  • AlenChen:有个问题,是不是header只能定义一次,没有办法像tableview一样定义多个,为什么我定义超过一个就崩呢
    David_Cap:@AlenChen 按道理来说不是得,你打个断点看看 哪里崩的。
  • ba482b7fcc0b:搞定了。xcode抽风可能 我新建了一个一模一样内容只是类名不一样的就可以了
    坑死了
    David_Cap:@尘世中的小码农 哈哈 写代码 都这样的啦,动不动就出现 莫名其妙的bug。
    ba482b7fcc0b:@David_Cap 坑死了 哎 我真应该试试重启
    David_Cap:@尘世中的小码农 ..............
  • ba482b7fcc0b:我在继承自 UICollectionReusableView的子类里添加了几个自定义的控件 然后运行时候出现duplicate symbol这个错误是为什么呢?
    //这个也是最重要的方法 获取Header的 方法。
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {

    NSString *CellIdentifier = @"habitHeaderView";
    //从缓存中获取 Headercell
    HabitHeaderView *cell = (HabitHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.section == 0) {
    cell.titleString = @"吃饭";
    }else{
    cell.titleString = @"睡觉";
    }
    return cell;
    }
    我这样写的
    Vampire丶Lv:还是想看一下header用代码自定义这块
    ba482b7fcc0b:@David_Cap 我试了下 只要添加了这个自定义的头视图就不对了,我把代码贴下麻烦你看看是不是自定义的时候出错了,第一个搞这个 :sob:
    David_Cap:@尘世中的小码农 你检查一下头文件什么的,里面有没有.m的东西。这个和CollectionView 基本没什么关系。
  • 小菜鸟一枚:为什么设置headerView的宽度是不起作用的呢?
    muchDrinkHotWat:@未完成的App 在flowLayout里改
    未完成的App:@小菜鸟一枚 怎么改宽度
    好棒大大:@小菜鸟一枚 宽带改不了 是屏宽

本文标题:CollectionView Header的使用

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