• UI控件之UICollectionView


    UICollectionView:集合视图,是iOS6.0后出现的,与UITableView类似,优势在于可以灵活的布局cell

    UICollectionViewLayout:布局类,抽象类,一般定义子类,iOS提供了一个具体的子类UICollectionViewFlowLayout,可以实现网格布局

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

    设置布局的滚动方式(垂直滚动)

    flow.scrollDirection=UICollectionViewScrollDirectionVertical;

    设置item之间的最小间隔(实际间隔由item的大小和UIEdgeInsets决定的)

    flow.minimumInteritemSpacing=10;

    设置每行之间的最小间隔

    flow.minimumLineSpacing=20;

    初始化对象时需指定布局对象

    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, 375, 603) collectionViewLayout:flow];

    设置代理

    _collectionView.dataSource=self;

    _collectionView.delegate=self;

    提前注册可重用单元格,@"MyCell":xib文件名,@"cell":xib文件中cell的identifier

    [_collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    提前注册每组的headerView,footerView (可重用的)

    [_collectionView registerClass:[CustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"supple"];

    [_collectionView registerClass:[CustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"supple"];

    设置系统不能自动调节布局

    self.automaticallyAdjustsScrollViewInsets=NO;

    设置背景色,默认是黑色

    _collectionView.backgroundColor=[UIColor whiteColor];

    UICollectionViewDataSouce的协议方法

    设置item的个数

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

    显示每项的方法

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

    {

        static NSString *identifier=@"cell";

        //从可重用队列中取出可重用的单元格项,如果没有,会自动按照之前注册过的样式创建

        MyCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

        cell.headerImageView.image=[UIImage imageNamed:@"0.png"];

        cell.titleLabel.text=@"test";

        return cell;

    }

    设置分组数,默认是1

    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;

    UICollectionViewDelegateFlowLayout的协议方法

    设置每个item的大小

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

    设置collectionView距上、左、下、右边侧的距离

    -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

    设置每组的headerView的大小,如果垂直滚动,宽度和collectionView宽度一样,需要设置高度,不实现的话默认是0

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

    设置每组的footerView的大小

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

    设置头尾视图

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

    {

        //可重用视图都是UICollectionReusableView的子类, 用来设置组的headerView(FooterView)

        CustomView *view;

        //显示每组的HeaderView

        if([kind isEqualToString:UICollectionElementKindSectionHeader]){

            //从可重用队列中取出view,已经提前注册过

            view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"supple" forIndexPath:indexPath];

            view.backgroundColor=[UIColor yellowColor];

        }//显示每组的footerView

        else if([kind isEqualToString:UICollectionElementKindSectionFooter]){

            view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"supple" forIndexPath:indexPath];

            view.backgroundColor=[UIColor blueColor];

        }

        return view;

    }

    UICollectionViewDelegateFlowLayout遵守了UICollectionViewDelegate协议,所以只要遵守UICollectionViewDelegateFlowLayout就可以访问两个协议的方法

    UICollectionViewDelegate的协议方法

    选中某项时执行的协议方法

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    {

        //获取用户选择的项的编号

        NSLog(@"%ld",indexPath.item);

    }

    http://www.cnblogs.com/PaulpauL/ 版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    Atitit flowable使用总结 目录 1. flowable 1 1.1. 添加依赖 1 1.2. Flowable的启动接口 2 2. 还是使用简单流程来完成业务流程的学习, 2 2.1.
    dell xps15 9550安装黑苹果
    显示器色域
    数据标准化的方法与意义
    XPS9550困扰我的散热问题终于解决了
    app开发
    纹理
    用 Java 开发一个打飞机小游戏(附完整源码)
    Spring Cloud Gateway 限流实战,终于有人写清楚了!
    OracleJDK 和 OpenJDK 有什么区别?来看看大神的回答!
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/4890103.html
Copyright © 2020-2023  润新知