CollectionView 简用
创建一个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 简用的更多相关文章
- Swift 使用CollectionView 实现图片轮播封装就是这样简单
前言: 这篇你可以学会自定义视图,创建collectionView,协议的使用,定时器; 自制图片 先上Demo:Github上封装好的下载即用, 好用请Star Thanks首先新建一个继承于UIV ...
- iOS之可拖拽重排的CollectionView
修复了拖拽滚动时抖动的一个bug,新增编辑模式,进入编辑模式后不用长按触发手势,且在开启抖动的情况下会自动进入抖动模式,如图: test.gif 图1:垂直滚动 drag1.gif 图2:水平滚动 d ...
- ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动
两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...
- CSharpGL(28)得到高精度可定制字形贴图的极简方法
CSharpGL(28)得到高精度可定制字形贴图的极简方法 回顾 以前我用SharpFont实现了解析TTF文件从而获取字形贴图的功能,并最终实现了用OpenGL渲染文字. 使用SharpFont,美 ...
- 用collectionview实现瀑布流-转(后面附demo,供参考)
算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...
- 用极简方式实现新浪新版本特性展示效果--view的图片轮播
在发布版本的时候,大多数软件会在第一次使用新版本时候弹出视图用几张图片给用户做一个新版本特性介绍,最简单如下图新浪的版本特性介绍 由于图片是全屏展示且是左右滑动,大多数情况开发者会选择使用scroll ...
- CollectionView水平和竖直瀑布流的实现
最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...
- tableViewCell嵌套collectionView,动态高度
方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来; 关键代码: vi ...
- iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调
UICollectionView的布局是可以自己定义的,在这篇博客中先在上篇博客的基础上进行扩充,我们先使用UICollectionViewFlowLayout,然后好好的介绍一下UICollecti ...
随机推荐
- sqlserver -- 学习笔记(六)日期格式转换
忘了这篇是从哪里看到,然后复制保存下来,感谢原创的分享~ ) ::08 ),'-',''),' ',''),':','') ),'/','-') ) , ) ) , ) ) , ) ) , ) ) , ...
- 一个ListView布局的不断演化
刚出来工作,就负责一个APP的某块功能的编写,该功能就是类似微博那样的界面.微博界面的编写实际上是非常复杂的,虽然它只是一个ListView,但要想让这个ListView滑得动,是的,在一些配置低的手 ...
- Python装饰器由浅入深
装饰器的功能在很多语言中都有,名字也不尽相同,其实它体现的是一种设计模式,强调的是开放封闭原则,更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也能装饰其他的对象,比如类,但通常,我们 ...
- 2013/11/22工作随笔-缓存是放在Model层还是放在Controller层
web网站的典型代码框架就是MVC架构,Model层负责数据获取,Controller层负责逻辑控制,View层则负责展示. 一般数据获取是去mysql中获取数据 但是这里有个问题,我们不会每次请求都 ...
- Device eth0 does not seem to be present,delaying initialization解决方法
Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization. 在linu ...
- mysql主从复制replication的一些相关命令
主服务器上的相关命令:show master status; mysql> show master status\G . row *************************** Posi ...
- C#字符串的恒定性
string str1="aa"; string str2="aa"; str1,str2,变量所指向的堆空间的地址是一样的.栈空间的内容是不一样的. //ne ...
- linq之into子句
在Linq表达式中,into子句可以创建一个临时标识符,使用该标识符可以存储group.join或select子句的结果. 下面实例中我们用GroupOtherQuery方法来演示group子句对结果 ...
- C# 实现WinForm窗口最小化到系统托盘代码,并且判断左右鼠标的事件
1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小 ...
- HTML页面导航栏页脚不动,变换中间部分
代码段一: <script> $(document).ready(function() { $("#teachingObjectives").click(functio ...