collectionView
//
/*
UICollectionView 类是iOS6 新引进的API,用于展示集合视图,
布局更加灵活,可实现多列布局,用法类似于UITableView类。
- 更新视图: [collectionView reloadData];
- 自定义UICollectionViewCell,与自定义tableViewCell基本一致
*/ #import "ViewController.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];//创建collectionView,要指定layout
collectionView.backgroundColor = [UIColor grayColor];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//注册UICollectionViewCell的重用标识,也就是下面我们要使用的cell
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册头视图(每组)
// [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"remnant"];//注册尾视图
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
}
//几个代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;//设置组数
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;//设置每组单元数,过多会自动换行
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//设置cell,注意,此处使用的identifier必须与之前注册的保持一致
static NSString *identifier = @"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
// cell.selectedBackgroundView = nil;设置选中视图
cell.layer.borderWidth = 0.5;
cell.layer.borderColor = [UIColor redColor].CGColor;
return cell;
}
//如果想要使用头视图,则必须实现该方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//设置每组头视图的尺寸
return CGSizeMake(, );
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%s",__FUNCTION__);
//设置头/根视图,注意,此处的withReuseIdentifier要与之前注册时使用的完全一致(注意重用机制导致的bug)
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor orangeColor];
[headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UILabel *lable = [[UILabel alloc]initWithFrame:headerView.bounds];
lable.text = [NSString stringWithFormat:@"%zi组的头视图",indexPath.section];
lable.textColor = [UIColor blueColor];
[headerView addSubview:lable];
return headerView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//否单元被点击
NSLog(@"%zi组,%zi列",indexPath.section,indexPath.item);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//设置item(视图元素)的尺寸
return CGSizeMake(, );
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//设置组的边距(上、左、下、右)
return UIEdgeInsetsMake(, , , );
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//两个item的列间距
return ;
} -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//如果一组中有多行item,设置行间距
return ;
}
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
// //设置每组尾视图的尺寸
// return CGSizeMake(100, 20);
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
collectionView的更多相关文章
- 用collectionview实现瀑布流-转(后面附demo,供参考)
算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...
- CollectionView水平和竖直瀑布流的实现
最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...
- tableViewCell嵌套collectionView,动态高度
方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来; 关键代码: vi ...
- iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调
UICollectionView的布局是可以自己定义的,在这篇博客中先在上篇博客的基础上进行扩充,我们先使用UICollectionViewFlowLayout,然后好好的介绍一下UICollecti ...
- WPF CollectionViewSource CollectionView
CollectionView 通俗讲就是可以对你绑定的集合可以进行 分组,排序 等功能 CollectionViewSource 根据字面意思是xxx的数据源 详细的介绍还是看 http://www ...
- ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用
做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...
- collectionview cell吸顶效果
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Hiragino Sans GB"; color: #cf8724 } ...
- collectionview使用
创建UICollectionViewFlowLayout 对象来设置相关的布局,包括itemSize,headerReferenceSize,sectionInset.设置对应的布局大小,相关的和顶部 ...
- ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...
随机推荐
- coursera 公开课 文本挖掘和分析(text mining and analytics) week 1 笔记
一.课程简介: text mining and analytics 是一门在coursera上的公开课,由美国伊利诺伊大学香槟分校(UIUC)计算机系教授 chengxiang zhai 讲授,公开课 ...
- Vware Workstation pro 12|虚拟机
Vmware是比较不错的PC虚拟化软件,vmware11+不在支持32的系统安装!体积比之前小了很多 VMware 12 官方中文页面 http://vmware.com/cn/products/wo ...
- BOP 2016 复赛题目
复赛采用团队赛的形式,基于Azure云计算平台在实际大数据的基础上展开前沿课题的较量. 在编程之美挑战赛复赛中,选手需要通过组队共同完成复赛题,考查选手编程能力的同时,也考验选手的团队协作能力.选手点 ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...
- C#高级特性_Lambda
“Lambda 表达式”(lambda expression)是一个匿名函数 省略delegate,甚至省略参数类型: 直接用(参数)=> {语句或表达式} 例如: button1.Click ...
- 1018Mysql分表分库
单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...
- Node.js项目目录介绍
新建的项目结构应该是这样 bin:项目的启动文件,也可以放其他脚本. node_modules:用来存放项目的依赖库. public:用来存放静态文件(css,js,img). routes:路由控制 ...
- 使用 screen 管理你的远程会话
文章转载自:https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 在此只作为笔记使用,不做他用 你是不是经常需要 SSH 或者 telent ...
- nginx实战2---浏览器设置缓存
对常见格式的图片文件等在浏览器本地缓存,对于css,js等文件在浏览器本地缓存. 语法:expires [time|epoch|max|off 默认值:off expires指令控制HTTP应答中的& ...
- .Net Core 1.0.0正式版安装及示例教程
使用VS Code 从零开始开发调试.NET Core 1.0 RTM. .NET Core 是一个开源的.跨平台的 .NET 实现. VS Code 全称是 Visual Studio Code,V ...