UICollectionView 简单应用和实际操作
系统自带的网格布局
UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
NSInteger itemWidth = (CGRectGetWidth(self.view.frame) - 4 * kMargin) / 3;
//设置单元格大小
flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth / 0.618);
//最小行间距(默认10)
flowLayout.minimumLineSpacing = 10;
//最小cell间距 (默认10)
flowLayout.minimumInteritemSpacing = 10;
//设置section的内边距
flowLayout.sectionInset = UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin);
设置UICollectionView的滑动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//sectionHeader的大小,如果是竖向滚动,只需设置Y值。如果是横向,只需设置X值。
flowLayout.headerReferenceSize = CGSizeMake(0, 200);
//网格布局
UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//设置数据源代理
collectionView.dataSource = self;
//注册cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
//注册sectionHeader
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
//多少分组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
//每一个分组里有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 200;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
//创建UICollectionViewCell的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//根据identifier从缓冲池里取cell
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
return cell;
}
//创建sectionHeader的方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//kind:种类,一共两种,一种是header,一种是footer
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView * reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
reusable.backgroundColor = [UIColor yellowColor];
return reusable;
}
return nil;
}
3、#pragma mark - UICollectionViewDelegate
//点击cell的方法 cell == item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"section - %@ , row - %@",@(indexPath.section),@(indexPath.row));
}
4、#pragma mark - UICollectionViewDelegateFlowLayout
设置itemSize,代理优先级高于属性
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return CGSizeMake(20, 20);
}
return CGSizeMake(10, 10);
return CGSizeMake(arc4random_uniform(100), arc4random_uniform(100));
}
设置sectionInset
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if (section == 0) {
return UIEdgeInsetsMake(20, 30, 40, 50);
}
return UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin);
}
设置minimumLineSpacing最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
if (section == 0) {
return 20;
}
return 2;
}
设置headersize
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if (section == 0) {
return CGSizeMake(0, 500);
}
return CGSizeMake(0, 100);
}
UICollectionView 简单应用和实际操作的更多相关文章
- Linq to SQL 简单的增删改操作
Linq to SQL 简单的增删改操作. 新建数据库表tbGuestBook.结构如下: 新建web项目,完成相应的dbml文件.留言页面布局如下 <body> <form id= ...
- 简单的redis缓存操作(get、put)
简单的redis缓存操作(get.put) 本文介绍简单的redis缓存操作,包括引入jedisjar包.配置redis.RedisDao需要的一些工具.向redis中放数据(put).从redis中 ...
- 【JavaScript】使用setInterval()函数作简单的轮询操作
轮询(Polling)是一种CPU决策怎样提供周边设备服务的方式,又称"程控输出入"(Programmed I/O). 轮询法的概念是.由CPU定时发出询问.依序询问每个周边设备是 ...
- 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- 使用KEIL C51实现的简单合作式多任务操作系统内核(单片机实现版本)
基于网上网友的代码,自己在单片机上实现, 特此记录分享之. 基于https://blog.csdn.net/yyx112358/article/details/78877523 //使用KEIL C5 ...
- 国产化之路-统信UOS + Nginx + Asp.Net MVC + EF Core 3.1 + 达梦DM8实现简单增删改查操作
专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...
- C++ //深拷贝与浅拷贝 //浅拷贝 : 简单的赋值拷贝操作 //深拷贝: 在堆区重新申请空间 进行拷贝操作
1 //深拷贝与浅拷贝 2 3 //浅拷贝 : 简单的赋值拷贝操作 4 //深拷贝: 在堆区重新申请空间 进行拷贝操作 5 6 7 #include <iostream> 8 using ...
- UICollectionView 简单使用
显示数据列表 大家通常使用的是UITableView 不用说TableView 是大家的首选.在iOS6之前这也是必选.但是伴随着APP的成长一起都在变化目前更多的呈现一种块状的显示效果.之前的行式显 ...
- 用php实现一个简单的链式操作
最近在读<php核心技术与最佳实践>这本书,书中第一章提到用__call()方法可以实现一个简单的字符串链式操作,比如,下面这个过滤字符串然后再求长度的操作,一般要这么写: strlen( ...
随机推荐
- linux 课后作业
第一章 第一单元 : 安装linux 系统:已完成 第二单元: 略 第三单元: 1) 要求以root用户登录系统,右击桌面打开终端,查看当前登陆Linux系统所使用的用户名2) 查看哪些用户在系统上工 ...
- ODBC 是什么
In computing, ODBC (Open Database Connectivity) is a standard programming language middleware API fo ...
- [置顶] 两台一级域名相同二级域名不同的服务器,怎么共享session
比如www.hongchangfirst.com和video.hongchangfirst.com两个域名,一级域名相同,二级域名不同.每个服务器运行着不同的功能模块或者不同的子系统,他们使用不同的二 ...
- UI进阶 科大讯飞(2) 语音合成(文字转换成语音)
科大讯飞开放平台.SDK下载.添加静态库.初始化见UI进阶 科大讯飞(1) 语音听写(语音转换成文字) 实现语音合成 功能实现步骤: 导入头文件 创建文字识别对象 指定文字识别后的回调代理对象 开启文 ...
- 1016. 部分A+B (15)
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...
- 基于jQuery的视频和音频播放器jPlayer
jPlayer见网络上资料很少,官方英文资料太坑爹TAT,于是就写一个手记给大家参考下.据我观察,jPlayer的原理主要是用到HTML5,在不支持HTML5的浏览器上使用SWF.做到全兼容,这一点很 ...
- easyui datagrid 的分页刷新按钮
datagrid 刷新bug: 情形: 当用户A,B 同时操作 datagrid时(记录1,记录2.记录3).如果A如果删除记录1, B此时已选中了记录1 ,记录2 , 这时B点击分页中的刷新按 ...
- js中有关滑动问题的一些理解
在做导航栏的时候,肯定会用到点击滑动的,但不知道大家有没有遇到过这种问题: $(window).animate({ scrollTop: "1000px" }) $(documen ...
- js 原型的内存分析
使用构造器的弊端:http://www.cnblogs.com/a757956132/p/5258897.html 示例 将行为设置为全局的行为,如果将所有的方法都设计为全局函数的时候, 这个函数就可 ...
- Spring+Struts+Ibatis的配置
指定Spring配置文件位置 <context-param> <param-name>contextConfigLocation</param-name> < ...