创建一个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. HTML注释引起的问题

    因为规范要求需要对页面进行说明,添加作者等信息,所以在cshtml的代码中添加了html注释,包括之前使用jsp也是这样做的: 在页面布局的时候,需要对高度进行动态计算,IE8以上没有问题,主要是在I ...

  2. rsync安装配置及故障解决完全教程[window, 文件同步]

    Rsync是的全称是: remote synchronize, 也就是远程同步数据, 它是一款不错的文件同步软件,而且是免费的, 它在镜像保存整个目录树和文件系统的同时保持原来文件的权限.时间.软硬链 ...

  3. Screen Space Subsurface Scatting(Skin Rendring)

    还差通透度计算,RenderMonkey截图. 参考: http://developer.download.nvidia.com/presentations/2007/gdc/Advanced_Ski ...

  4. .Net魔法堂:史上最全的ActiveX开发教程——ActiveX与JS间交互篇

    一.前言 经过上几篇的学习,现在我们已经掌握了ActiveX的整个开发过程,但要发挥ActiveX的真正威力,必须依靠JS.下面一起来学习吧! 二.JS调用ActiveX方法 只需在UserContr ...

  5. 奥特曼小分队之四(Work Breakdown Structure)

    写在前面的话:游戏介绍http://www.cnblogs.com/atmxfd/p/5415107.html 需求 我们的游戏是一款基于局域网的游戏,用户只需将服务端和客户端置于同一局域网下即可使用 ...

  6. sns社区架构设计案例分享(二)

    源码下载地址:http://www.jinhusns.com/Products/Download/?type=xcj 五. 架构使用说明 > 缓存 > 使用说明 > (一)基础类库介 ...

  7. foreach---集合已修改;可能无法执行枚举操作。

    小结 : foreach是取只读的,在取的时候数据不能变(包括修改,删除,添加等).要避免这个问题,就应该使用for循环--- 原因: 当用foreach遍历Collection时,如果对Collec ...

  8. datagrid动态数据添加超链接的方法

    首先,我我们要有一个json格式的datagrid_data.json文件,如下:

  9. 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar

    [源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  10. IE Unknown runtime error

    1. 在函数中使用原生的js的时候,有时在IE下会出现Unknown runtime error 火狐下正常 2. 解决办法, 将原生js改成jquery处理兼容问题 document.getElem ...