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( ...
随机推荐
- POJ1384Piggy-Bank(DP)
POJ1384http://poj.org/problem?id=1384 最简单的完全背包问题,注意下初始化为INF就可以. #include <map> #include <se ...
- C# Devexpress学习--LabelControl
A LabelControl can display an image (regular or animated GIF file). Different images can be provided ...
- 使用XCopy发布网页
链接:https://documentation.devexpress.com/#eXpressAppFramework/CustomDocument113245 In this lesson, yo ...
- CXF 与Spring整合配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...
- iOS开发-UIColor转UIIamge方法
只能说太神奇了,完美应用到我的毕业设计 - (UIImage*) createImageWithColor: (UIColor*) color { CGRect rect=CGRectMake(,,s ...
- Java常见排序算法之冒泡排序
在学习算法的过程中,我们难免会接触很多和排序相关的算法.总而言之,对于任何编程人员来说,基本的排序算法是必须要掌握的. 从今天开始,我们将要进行基本的排序算法的讲解.Are you ready?Let ...
- 获取 CPU 序列号
function GetCpuID: string; var _eax, _ebx, _ecx, _edx: Longword; s, s1, s2: string; begin asm push e ...
- 2016 icpc-camp 之旅(一)
啦啦啦,终于前往icpccamp啦! 嗯,该死的飞机居然晚点了! 诶,晚点居然还会发赔偿金! 飞机上没什么好说的,和萌神一起看了5集龙与虎,然后就到了. 讲道理,海南航空感觉一般. 我的座位前面有个平 ...
- C#操作Excel(1)Excel对象模型
Excel对象模型 (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Excel的应用程序 本文的PDF下载地址:C#操作Excel2007.pdf ...
- 关于OPenGL和OSG的矩阵 (转)
关于OPenGL和OSG的矩阵 矩阵真的是一个很神奇的数学工具, 虽然单纯从数学上看, 它并没有什么特别的意义, 但一旦用到空间中的坐标变换,它就“一遇风云便成龙”, 大显神威了.简单的工具实现了复杂 ...