创建一个CollectionView 分为几个步骤

1.先创建布局FlowLayout 设置布局格式

2.创建CollectionView 并使用布局Flowlayout  -initWithFrame: collectionViewLayout:

3.遵循协议delegate dataSource

4.注册cell

5.添加到view

#import "ViewController.h"
#import "reusableview.h"

- (void)viewDidLoad {
    [super viewDidLoad];
   //创建布局
    UICollectionViewFlowLayout * FL = [[UICollectionViewFlowLayout alloc]init];

//设置布局格式
    [FL setScrollDirection:UICollectionViewScrollDirectionVertical];

// 设置分组头大小
    FL.headerReferenceSize = CGSizeMake(100, 50);
  //创建collectionview 并且 遵循FL格式
    UICollectionView * collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:FL ];
    
    collect.delegate = self;
    collect.dataSource = self;
   //注册uicollectionviewcell
    [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

//注册    重写uicollectionreusableView  复用类,只复用类中内容
    [collect registerClass:[reusableview class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    
    [self.view addSubview:collect];
}
#pragma make - dataSource
//cell 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}
//设置分组个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
//cell 设定
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusable = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        
        reusableview *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        view.title.text = [[NSString alloc] initWithFormat:@"头部视图%ld",indexPath.section];
        reusable = view;
    }
    return reusable;
}
#pragma make - FlowLayout
//定义cell 的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(96, 100);
}
//cell上下间距的大小
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 25;
}
//定义每个uicollectionview 的marger
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake((HEIGHT-100*3-50)/2, (WIDTH-96*2-30)/2, (HEIGHT-100*3-50)/2, (WIDTH-96*2-30)/2);
}

CollectionView 简用的更多相关文章

  1. Swift 使用CollectionView 实现图片轮播封装就是这样简单

    前言: 这篇你可以学会自定义视图,创建collectionView,协议的使用,定时器; 自制图片 先上Demo:Github上封装好的下载即用, 好用请Star Thanks首先新建一个继承于UIV ...

  2. iOS之可拖拽重排的CollectionView

    修复了拖拽滚动时抖动的一个bug,新增编辑模式,进入编辑模式后不用长按触发手势,且在开启抖动的情况下会自动进入抖动模式,如图: test.gif 图1:垂直滚动 drag1.gif 图2:水平滚动 d ...

  3. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动

    两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...

  4. CSharpGL(28)得到高精度可定制字形贴图的极简方法

    CSharpGL(28)得到高精度可定制字形贴图的极简方法 回顾 以前我用SharpFont实现了解析TTF文件从而获取字形贴图的功能,并最终实现了用OpenGL渲染文字. 使用SharpFont,美 ...

  5. 用collectionview实现瀑布流-转(后面附demo,供参考)

    算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...

  6. 用极简方式实现新浪新版本特性展示效果--view的图片轮播

    在发布版本的时候,大多数软件会在第一次使用新版本时候弹出视图用几张图片给用户做一个新版本特性介绍,最简单如下图新浪的版本特性介绍 由于图片是全屏展示且是左右滑动,大多数情况开发者会选择使用scroll ...

  7. CollectionView水平和竖直瀑布流的实现

    最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...

  8. tableViewCell嵌套collectionView,动态高度

    方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来; 关键代码: vi ...

  9. iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调

    UICollectionView的布局是可以自己定义的,在这篇博客中先在上篇博客的基础上进行扩充,我们先使用UICollectionViewFlowLayout,然后好好的介绍一下UICollecti ...

随机推荐

  1. Mina、Netty、Twisted一起学(三):TCP消息固定大小的前缀(Header)

    在上一篇博文中,有介绍到用换行符分割消息的方法.但是这种方法有个小问题,如果消息中本身就包含换行符,那将会将这条消息分割成两条,结果就不对了. 本文介绍另外一种消息分割方式,即上一篇博文中讲的第2条: ...

  2. Character Controller (角色控制器) 中 Move()和SimpleMove() 的区别

    首先给出两者的圣典: CollisionFlagsMove(Vector3motion); Description A more complex move function taking absolu ...

  3. iOS-图片轮播-SDCycleSCrollView的使用

    介绍: SDCycleScrollView 是一款非常强大的轮播图第三方. 轮播流畅,手滑流畅.使用方便.自定义简单. 可以更改pageControl. 一. Demo地址 https://pan.b ...

  4. js获取html5 audio 音频时长方法

    <audio src="我的好兄弟.mp3" controls="controls"  id="audio" style=" ...

  5. JS&CSS文件请求合并及压缩处理研究(三)

    上篇我们进行了一些代码方面的准备工作.接下来的逻辑是:在View页面解析时,通过 Html.AppendResFile 方法添加的资源文件,我们需要按照分组.优先级,文件名等条件,对其路径进行合并.具 ...

  6. js 各种数值类型正则匹配检测

    随拿随用只js正则表达式,反正平时工作我是不写正则的,大神请自动绕行: 验证数字的正则表达式集验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数 ...

  7. MySQL+Keepalived实现双机HA

    host1与host3互为主从,即host1为host3的主机,同时也为host3的从机   host1 192.168.203.131 host2 192.168.203.132 host3 192 ...

  8. JS中 toString() & valueOf()

    数据的转换 所有对象继承了两个转换方法: 第一个是toString(),它的作用是返回一个反映这个对象的字符串 第二个是valueOf(),它的作用是返回它相应的原始值 toString() toSt ...

  9. 重构第9天:提取接口(Extract Interface)

    理解:提取接口的意思是,多于一个类共同使用某个类中的方法或属性,那么我们可以把这些方法和属性提出来,作为一个单独的接口.这样的好处是解除代码间的依赖,降低耦合性. 详解: 先看重构前的代码: publ ...

  10. Gridview的RowDataBound事件(添加删除提示,改变背景颜色)

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.Row ...