• UICollectionView显示header和footer


    在使用collectionView时,想做个分组,需要显示header,但是发现,显示header和footer的回调方法不执行,不懈追踪,终于找到原因!!!!!!!

        layout.headerReferenceSize = CGSizeMake(100, 22);//重要,写上该行代码后,显示header和footer的回调方法成功执行,最终成功显示header

    现在记录一下CollectionView的使用过程

    初始化:

       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

     [layout setScrollDirection:UICollectionViewScrollDirectionVertical];

       layout.headerReferenceSize = CGSizeMake(100, 22);

       UICollectionView *dataListcollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64,self.view.frame.size.width,260) collectionViewLayout:layout];

        [dataListcollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//注册cell

        [dataListcollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册header的view

        dataListcollectionView.delegate = self;

        dataListcollectionView.dataSource = self;

        [self.view addSubview:dataListcollectionView];

     设置代理方法

    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

    {

        return 1;

    }

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

    {

        return [dataArray count];

    }

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    {

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        return cell;

    }

     //显示header和footer的回调方法

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

    {

        if (kind == UICollectionElementKindSectionHeader)

     {//如果想要自定义header,只需要定义UICollectionReusableView的子类A,然后在该处使用,注意AIdentifier要设为注册的字符串,此处为“header”

            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

            return view;

        }

        return nil;

    }

    //设置元素大小

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

            return CGSizeMake(100,100);

    }

     

  • 相关阅读:
    MySQL Thread Pool: Problem Definition
    MySQL数据库InnoDB存储引擎多版本控制(MVCC)实现原理分析
    Mysql源码目录结构
    android学习18——对PathMeasure中getPosTan的理解
    android学习17——命令行建gradle工程
    android学习16——library project的使用
    android学习13——android egl hello world
    ant编译java的例子
    android学习12——重载SurfaceView一些方法的执行顺序
    Visual Studio命令行创建库文件lib
  • 原文地址:https://www.cnblogs.com/guatiantian/p/4239615.html
Copyright © 2020-2023  润新知