IOS UICollectionView基础+UICollectionViewFlowLayout基础
UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionViewFlowLayout当然也是必不可少的了,还是老样子结合代码进行简单介绍,首先看一下UICollectionView实现结果:
实现这些功能很简单,代码量极少,线看一下代码,然后进行深入了解:
//
// ViewController.m
// CX-UICollentionVIew基础
//
// Created by ma c on 16/3/19.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" static NSString * identifier = @"cxCellID";
@interface ViewController ()<UICollectionViewDataSource> @property (nonatomic, strong) UICollectionView * collectionView; @end @implementation ViewController
#pragma mark - set_and_get
-(UICollectionView *)collectionView{
if (!_collectionView) {
//自动网格布局
UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
//网格布局
_collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//注册cell
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
//设置数据源代理
_collectionView.dataSource = self;
}
return _collectionView; }
#pragma mark - life
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.collectionView]; }
#pragma mark - deleDate
//有多少的分组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return ; }
//每个分组里有多少个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ //根据identifier从缓冲池里去出cell
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor orangeColor]; return cell; } @end
介绍过最最基本的知识点之后,按着步骤来,进行进一步介绍。
UICollectionViewFlowLayout的使用让UICollectionView更加丰富多彩,下面看一下使用UICollectionViewFlowLayout之后的效果吧->
代码->
//
// ViewController.m
// CX-UICollentionVIew基础
//
// Created by ma c on 16/3/19.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" static NSString * identifier = @"cxCellID";
static CGFloat kMagin = .f;
static NSString * headIdentifier = @"cxHeadID";
@interface ViewController ()<UICollectionViewDataSource> @property (nonatomic, strong) UICollectionView * collectionView; @end @implementation ViewController
#pragma mark - set_and_get
-(UICollectionView *)collectionView{
if (!_collectionView) {
//自动网格布局
UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init]; CGFloat itemWidth = (self.view.frame.size.width - * kMagin) / ; //设置单元格大小
flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth / 0.618);
//最小行间距(默认为10)
flowLayout.minimumLineSpacing = ;
//最小item间距(默认为10)
flowLayout.minimumInteritemSpacing = ;
//设置senction的内边距
flowLayout.sectionInset = UIEdgeInsetsMake(kMagin, kMagin, kMagin, kMagin);
//设置UICollectionView的滑动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//sectionHeader的大小,如果是竖向滚动,只需设置Y值。如果是横向,只需设置X值。
flowLayout.headerReferenceSize = CGSizeMake(,);
//网格布局
_collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//注册cell
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
//注册header
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headIdentifier];
//设置数据源代理
_collectionView.dataSource = self;
}
return _collectionView; }
#pragma mark - life
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.collectionView]; }
#pragma mark - deleDate
//有多少的分组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return ; }
//每个分组里有多少个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ //根据identifier从缓冲池里去出cell
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor orangeColor]; return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ //kind有两种 一种时header 一种事footer
if (kind == UICollectionElementKindSectionHeader) { UICollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headIdentifier forIndexPath:indexPath]; header.backgroundColor = [UIColor yellowColor]; return header; }
return nil; } @end
灵活的是使用 UICollectionView+UICollectionViewFlowLayout 会带来更多的有趣的体验,随后我会介绍一些比较实用的实现。
IOS UICollectionView基础+UICollectionViewFlowLayout基础的更多相关文章
- iOS 9应用开发基础教程下册
iOS 9应用开发基础教程下册 介绍: 本教程是国内第一本iOS 9开发应用教程.本教程基于Xcode 7.0,使用Swift 2.0语言讲解如何开发iOS 9的应用App. 学习建议:本教程针对 ...
- iOS 开发之 GCD 基础
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- [.net 面向对象编程基础] (3) 基础中的基础——数据类型
[.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...
- [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换
[.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...
- [.net 面向对象编程基础] (5) 基础中的基础——变量和常量
[.net面向对象编程基础] (5) 基础中的基础——变量和常量 1.常量:在编译时其值能够确定,并且程序运行过程中值不发生变化的量. 通俗来说,就是定义一个不能改变值的量.既然不能变动值,那就必须 ...
- [.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式
[.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式 说起C#运算符和表达式,小伙伴们肯定以为很简单,其实要用好表达式,不是一件容易的事.一个好的表达式可以让你做事半功倍的效果,比如 ...
- [.net 面向对象编程基础] (7) 基础中的基础——流程控制语句
[.net 面向对象编程基础] (7) 基础中的基础——流程控制语句 本来没有这一节的内容,后来考虑到既然是一个系列文章,那么就尽可能写的详细一些,本节参考了网上朋友所写的例子,为的是让更多小伙伴学习 ...
- [.net 面向对象编程基础] (8) 基础中的基础——修饰符
[.net 面向对象编程基础] (8) 基础中的基础——修饰符 在进入C#面向对象核心之前,我们需要先对修饰符有所了解,其实我们在前面说到变量和常量的时候,已经使用了修饰符,并且说明了变量和常量的修改 ...
- JavaScript基础---语言基础(1)
写在前面: 通过四篇博客把JS基础中的基础整理一下,方便自己查阅,这些内容对于实际项目开发中也许并不会在意,但是作为JS的语言基础,自觉还是应该熟悉.在完成这三篇博客(JavaScript基础---语 ...
随机推荐
- Unity 摄像机Clear Flags和Culling Mask属性用途详解
原文地址:http://blog.csdn.net/tanmengwen/article/details/8798231 1.简述两个属性 1.1 Clear Flags 清除标记 每个相机在渲染时会 ...
- 栈stack的C实现
头文件—————————————————————————— #ifndef _STACK_H_ #define _STACK_H_ #include <stdlib.h> #define ...
- 伸展树(三)之 Java的实现
概要 前面分别通过C和C++实现了伸展树,本章给出伸展树的Java版本.基本算法和原理都与前两章一样.1. 伸展树的介绍2. 伸展树的Java实现(完整源码)3. 伸展树的Java测试程序 转载请注明 ...
- 在VS中使用TinyFox调试OWIN应用(转)
在很早一段时间之前,我曾经写过一篇关于Katana的使用方法的文章<如何安装并简单的使用OwinHost——Katana>,上面就有介绍如何在VS中调试使用Katana作为Host的App ...
- ADO.NET学习系列(三)----做一个登录案例
总体思路.根据用户输入的用户名和密码,来判断,和数据库里面存的是不是一样,如果一样就表明登录成功,否则就登录失败. 方案一: 1.select* from 表名 where username=&quo ...
- 重构第9天:提取接口(Extract Interface)
理解:提取接口的意思是,多于一个类共同使用某个类中的方法或属性,那么我们可以把这些方法和属性提出来,作为一个单独的接口.这样的好处是解除代码间的依赖,降低耦合性. 详解: 先看重构前的代码: publ ...
- ViewData和TempData以及Session的小结
ViewData一般用在从控制器向页面上传递数据. Public ActionResult Show() { ViewData["message"]="你好"; ...
- 重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换
[源码下载] 重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换 作者:webabcd 介绍 ...
- WinFrom子窗体向父窗体传值
父窗框mainForm;子窗体childForm,利用事件进行传值 在子窗体中的操作: public event EventHandler accept;public string value; pr ...
- ElasticSearch文档-简单介绍
ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引 ...