1、网格视图
 
UICollectionView      网格布局
UICollectionViewFlowLayout系统图自带网格布局

系统自带的网格布局

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];

2、UICollectionView 数据源

//多少分组

- (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 简单应用和实际操作的更多相关文章

  1. Linq to SQL 简单的增删改操作

    Linq to SQL 简单的增删改操作. 新建数据库表tbGuestBook.结构如下: 新建web项目,完成相应的dbml文件.留言页面布局如下 <body> <form id= ...

  2. 简单的redis缓存操作(get、put)

    简单的redis缓存操作(get.put) 本文介绍简单的redis缓存操作,包括引入jedisjar包.配置redis.RedisDao需要的一些工具.向redis中放数据(put).从redis中 ...

  3. 【JavaScript】使用setInterval()函数作简单的轮询操作

    轮询(Polling)是一种CPU决策怎样提供周边设备服务的方式,又称"程控输出入"(Programmed I/O). 轮询法的概念是.由CPU定时发出询问.依序询问每个周边设备是 ...

  4. 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  5. 使用KEIL C51实现的简单合作式多任务操作系统内核(单片机实现版本)

    基于网上网友的代码,自己在单片机上实现, 特此记录分享之. 基于https://blog.csdn.net/yyx112358/article/details/78877523 //使用KEIL C5 ...

  6. 国产化之路-统信UOS + Nginx + Asp.Net MVC + EF Core 3.1 + 达梦DM8实现简单增删改查操作

    专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...

  7. C++ //深拷贝与浅拷贝 //浅拷贝 : 简单的赋值拷贝操作 //深拷贝: 在堆区重新申请空间 进行拷贝操作

    1 //深拷贝与浅拷贝 2 3 //浅拷贝 : 简单的赋值拷贝操作 4 //深拷贝: 在堆区重新申请空间 进行拷贝操作 5 6 7 #include <iostream> 8 using ...

  8. UICollectionView 简单使用

    显示数据列表 大家通常使用的是UITableView 不用说TableView 是大家的首选.在iOS6之前这也是必选.但是伴随着APP的成长一起都在变化目前更多的呈现一种块状的显示效果.之前的行式显 ...

  9. 用php实现一个简单的链式操作

    最近在读<php核心技术与最佳实践>这本书,书中第一章提到用__call()方法可以实现一个简单的字符串链式操作,比如,下面这个过滤字符串然后再求长度的操作,一般要这么写: strlen( ...

随机推荐

  1. python 遍历删除日志

    #! /usr/bin/python2.6#-*- encoding:UTF-8 -*- import osimport os.pathimport time root_dir = os.getcwd ...

  2. JQuery点击收起,点击展开以及部分非空小验证

    <tr> <td nowrap align="right" width="18%"> 解决方案: </td> <td ...

  3. 30几个HTML5经典动画应用回顾 让你大饱眼福

    周末大放送,让我们来回顾一下HTML5经典动画应用,一定会让你大饱眼福. 1.HTML5 Canvas画板画图工具 可定义笔刷和画布 HTML5 Canvas还有一个比较实用的应用,那就是网络画板,这 ...

  4. kindeditor html代码过滤不能保存

    这是因为编辑器默认开启了过滤模式(filterMode:true).当filterMode为true时,编辑器会根据htmlTags设定自动过滤HTML代码,主要是为了生成干净的代码.如果想保留所有H ...

  5. easyui datagrid 的分页刷新按钮

    datagrid  刷新bug: 情形: 当用户A,B  同时操作 datagrid时(记录1,记录2.记录3).如果A如果删除记录1,  B此时已选中了记录1 ,记录2 , 这时B点击分页中的刷新按 ...

  6. Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口

    1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...

  7. jquery下拉框实现将左边的选项添加到右边区域

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. linux 安装Apache----tar.gz文件安装方式(零环境安装)

    一.下载apache源代码 1.下载地址:http://httpd.apache.org/download.cgi  找稳定的最新的版本(Stable Release) 得到文件 httpd-2.4. ...

  9. Java中static的用法

    static静态,作为修饰符,最初是由c引入,一开始static表示退出一个块后依然存在的局部变量.随后,static表示不能被其他文件访问的全局变量和函数.到了C++和java,static表示属于 ...

  10. 如何自学Java 经典

    JAVA自学之路 JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3G.测试等. 那么究竟 ...