UICollectionView 基础
在iOS开发中经常会用到UICollectionView,和UITableView同样即成UIScrollView 但是操作起来比UITableVIew要麻烦一些 ,有些地方需要注意,一下是UICollectionView基础详解。
//
// ViewController.m
// Collection
#import "ViewController.h"
#import "CollectionViewCell.h"
#import "CollectionViewCell1.h"
#import "CollectionReusableView.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//此处必须要有创见一个UICollectionViewFlowLayout的对象
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
//同一行相邻两个cell的最小间距
layout.minimumInteritemSpacing = ;
//最小两行之间的间距
layout.minimumLineSpacing = ;
_collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(, , , ) collectionViewLayout:layout];
_collectionView.backgroundColor=[UIColor whiteColor];
_collectionView.delegate=self;
_collectionView.dataSource=self;
//这个是横向滑动
//layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
[self.view addSubview:_collectionView];
/*
*这是重点 必须注册cell
*/
//这种是xib建的cell 需要这么注册
UINib *cellNib=[UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
[_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CollectionViewCell"];
//这种是自定义cell不带xib的注册
// [_collectionView registerClass:[CollectionViewCell1 class] forCellWithReuseIdentifier:@"myheheIdentifier"];
//这种是原生cell的注册
// [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//这是头部与脚部的注册
UINib *cellNib1=[UINib nibWithNibName:@"CollectionReusableView" bundle:nil];
[_collectionView registerNib:cellNib1 forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"CollectionReusableView"];
[_collectionView registerNib:cellNib1 forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionReusableView"];
}
//一共有多少个组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return ;
}
//每一组有多少个cell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return ;
}
//每一个cell是什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
cell.label.text=[NSString stringWithFormat:@"%ld",indexPath.section*+indexPath.row];
cell.backgroundColor=[UIColor groupTableViewBackgroundColor];
return cell;
}
//头部和脚部的加载
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *view=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView"forIndexPath:indexPath];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
label.text=@"头";
}else{
label.text=@"脚";
}
[view addSubview:label];
return view;
}
//每一个分组的上左下右间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(, , , );
}
//头部试图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(, );
}
//脚部试图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(, );
}
//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(, );
}
//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//cell被点击后移动的动画
[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
}
@end
UICollectionView 基础的更多相关文章
- IOS UICollectionView基础+UICollectionViewFlowLayout基础
UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionVi ...
- UICollectionView基础学习
相信了解UICollectionView的也一定听过瀑布流吧,开始之前先提供两个瀑布流,有时间的可以深入研究研究 https://github.com/dingpuyu/WaterFall https ...
- UICollectionView基础/UICollectionViewCell的四种创建方式
前言 UICollectionViewCell的四种创建方式:http://blog.csdn.net/ZC_Huang/article/details/52002302 这个控件,看起来与UITab ...
- UICollectionView基础
初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...
- iOS UICollectionView基础
转载自:http://www.cnblogs.com/wayne23/p/4013522.html 初始化部分: UICollectionViewFlowLayout *flowLayout= [[ ...
- UICollectionView基础API笔记
UICollectionView系列API,属性含义笔记.在UICollectionView笔记1中我们了解了UICollectionView是什么,以及可以做什么:在UICollectionView ...
- UICollectionView基础用法
初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...
- UI:UICollectionView
#import "ViewController.h" #import "HeaderView.h" #import "FooterView.h&quo ...
- UICollectionView——整体总结
前言 这几天有时间看了下UICollectionView的东西,才发觉它真的非常强大,很有必要好好学习学习.以前虽然用过几次,但没有系统的整理总结过.这两天我为UICollectionView做一个比 ...
随机推荐
- 使用Laya引擎开发微信小游戏(下)
本文由云+社区发表 6. 动画 6.1 创建伞兵对象 在src目录下创建一个新目录role,用来存放游戏中角色. 在role里创建一个伞兵Soldier.ts对象文件. module role{ ex ...
- RabbitMQ 消息中间件
RabbitMQ 是使用 Erlang 语言开发的消息中间件, 其遵循了高级消息队列协议(Advanced Message Queuing Protocol, AMQP). 与 Kafka 等消息队列 ...
- Python数值类型
数值类型 python的数值类型包括常规的类型:整数(没有小数部分的数字).浮点数(通俗地说,就是有小数部分的数字)以及其它数值类型(复数.分数.有理数.无理数.集合.进制数等).除了十进制整数,还有 ...
- laypage 物理分页与逻辑分页实例
前言 以下介绍摘自 layui官网laypage layPage 致力于提供极致的分页逻辑,既可轻松胜任异步分页,也可作为页面刷新式分页.自 layui 2.0 开始,无论是从核心代码还是API设计, ...
- [转]Cordova - 彻底搞定IOS编译!
本文转自:https://www.cnblogs.com/sunylat/p/9946482.html 操作系统:OSX10.14 XCode:10.1 Cordova:8.1.2 假设已经配置好了C ...
- Spring Day 2
**Spring框架的IOC之注解方式的快速入门** 步骤一:导入注解开发所有需要的jar包 步骤二:创建对应的包结构,编写Java的类:接口到实现类 步骤三:在src的目录下,创建applicati ...
- swoole扩展实现真正的数据库连接池
php的数据库连接池一直以来都是一个难题,很多从php语言转向java的项目,大多数原因可能都是因为java有更好的连接池实现.php的mysql扩展提供了长连接的API,但在php机器数量较多,规模 ...
- Java_Comparable,Comparator两接口区别
Comparable和Comparator的区别 根本区别 1.Comparable是一个内比较器,Comparator是一个外比较器 封装的包不同 java.util.Comparator java ...
- python中的tcp
目录 TCP简介 TCP介绍 TCP特点 TCP与UDP的不同点 udp通信模型 TCP通信模型 tcp客户端 tcp服务器 tcp注意点 TCP简介 TCP介绍 TCP协议,传输控制协议(英语:Tr ...
- 6.方法_EJ
第38条: 检查参数的有效性 对于这一条,最常见的莫过于检查参数是否为null. 有时出现调用方未检查传入的参数是否为空,同时被调用方也没有检查参数是否为空,结果这就导致两边都没检查以至于出现null ...