zoukankan      html  css  js  c++  java
  • 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/ 版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    有关css的效果展示
    php将代码上传至服务器步骤
    php连接数据库
    PHP如何从一个页面跳转到另一个页面
    Windows计算器
    查询本地正在运行的端口号
    wamp虚拟路径访问配置
    sublime text3 Emmet (原zenCoding)安装方法
    Latex(一)公式自动编号与自动引用
    Linux下Django开发学习(一)
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/4890103.html
Copyright © 2011-2022 走看看