OC 基础 UICollectionView
一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
下面介绍一下collectionview的常用属性跟方法。‘
初始化cell的样式
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
self.flowLayout = flowLayout;
collectionView实例化对象和大小
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
self.collectionView = collectionView;
[self.view addSubview:self.collectionView];
UICollectionViewScrollDirectionVertical, 竖向排布
UICollectionViewScrollDirectionHorizontal 横向排布
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
设置cell的大小
self.flowLayout.itemSize = CGSizeMake(400, 300);
设置cell的上下间距
self.flowLayout.minimumLineSpacing = 100;
设置cell的左右间距
self.flowLayout.minimumInteritemSpacing = 10;
设置cell的内间距
self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
设置cell的头部高度
self.flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
设置cell的低部高度
self.flowLayout.footerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
self.collectionView.collectionViewLayout = self.flowLayout;
设置背景颜色
self.collectionView.backgroundColor = [UIColor whiteColor];
设置分页效果
self.collectionView.pagingEnabled = YES;
设置代理
self.collectionView.delegate = self;
设置数据源
self.collectionView.dataSource = self;
是否显示竖向滚动条
self.collectionView.showsVerticalScrollIndicator = NO;
是否显示横向滚动条
self.collectionView.showsHorizontalScrollIndicator = NO;
注册cell
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"kMyCollectionCell"];
注册HeaderView
[self.collectionView registerClass:[HeaderCollectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
注册FooterView
[self.collectionView registerClass:[FooterCollectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
刷新对应的列
[self.collectionView performBatchUpdates:^{[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];
实现代理方法跟数据源方法
#pragma mark - 列数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{return 1;
}#pragma mark - 行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{return 20;
}#pragma mark - 具体实现cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kMyCollectionCell" forIndexPath:indexPath];return cell;
}#pragma mark - 点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"indexPath.row -- %ld",(long)indexPath.row);
}#pragma mark - 设置头部view跟底部view
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{if (kind == UICollectionElementKindSectionHeader) {HeaderCollectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];headerView.value = @"headerView";return headerView;}else{FooterCollectionView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerView" forIndexPath:indexPath];footerView.value = @"footerView";return footerView;}
}#pragma mark - 设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{return CGSizeMake(400, 400);
}#pragma mark - 设置cell的内间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{return UIEdgeInsetsMake(5, 5, 5, 5);
}#pragma mark - 设置cell的上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{return 100;
}#pragma mark - 设置cell的左右间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{return 10;
}#pragma mark - 设置cell的头部高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{return CGSizeMake(self.view.bounds.size.width, 100);
}#pragma mark - 设置cell的脚部高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{return CGSizeMake(self.view.bounds.size.width, 100);
}
自定义cell
.h
#import <UIKit/UIKit.h>@interface MyCollectionCell : UICollectionViewCell
@property (nonatomic,copy)NSString *title;
@end
.m
#import "MyCollectionCell.h"
@interface MyCollectionCell()
@property (nonatomic, strong)UILabel *nameLabel;
@end@implementation MyCollectionCell- (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self){[self buildUI];}return self;
}- (void)buildUI {}@end
说明一下:使用collectionview的时候如果每一个collectionviewCell的大小是一致的话。我们可以创建的时候统一设置初始化好它的大小。如下面的代码。这样子写了之后就不用实现数据源的方法了。
//初始化cell的样式
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
self.flowLayout = flowLayout;
//collectionView实例化对象和大小
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
self.collectionView = collectionView;
[self.view addSubview:self.collectionView];/*UICollectionViewScrollDirectionVertical, 竖向排布UICollectionViewScrollDirectionHorizontal 横向排布*/
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
/*设置cell的大小 */
self.flowLayout.itemSize = CGSizeMake(400, 300);
/*设置cell的上下间距*/
self.flowLayout.minimumLineSpacing = 100;
/*设置cell的左右间距*/
self.flowLayout.minimumInteritemSpacing = 10;
/*设置cell的内间距*/
self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
/*设置cell的头部高度*/
self.flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
/*设置cell的低部高度*/
self.flowLayout.footerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
self.collectionView.collectionViewLayout = self.flowLayout;
如果collectionviewcell的大小每个都不一样的话才实现数据源的方法,就不用写上面的代码了。如下面的代码。
#pragma mark - 设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{return CGSizeMake(400, 400);
}#pragma mark - 设置cell的内间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{return UIEdgeInsetsMake(5, 5, 5, 5);
}#pragma mark - 设置cell的上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{return 100;
}#pragma mark - 设置cell的左右间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{return 10;
}
需求
collectionView后面加一个背景
例如:加一张图片,或者渐变颜色的view
self.collectionBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Phone_Width, Phone_Height)];
[self.collectionBgView az_setGradientBackgroundWithColors:@[[UIColor colorWithHex: 0xA3F6D3],[UIColor colorWithHex:0xF40047]] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
self.collectionView.backgroundView = self.collectionBgView;
OC 基础 UICollectionView相关推荐
- OC基础第四讲--字符串、数组、字典、集合的常用方法
OC基础第四讲--字符串.数组.字典.集合的常用方法 字符串.数组.字典.集合有可变和不可变之分.以字符串为例,不可变字符串本身值不能改变,必须要用相应类型来接收返回值:而可变字符串调用相应地方法后, ...
- OC基础回想(十二)协议
在OC基础(十一)中我们讨论了类别和非正式协议的奇异之处.在使用非正式协议时.能够仅仅实现你想要获得响应的方法.也不必在对象中声明不论什么内容来表示该对象可用作托付对象. 全部这些任务能够用最少的代码 ...
- OC基础 代理和协议
OC基础 代理和协议 1.协议 (1)oc语言中得协议:一组方法列表,不需要我们自己实现,由遵守协议的类来实现协议所定制的方法. (2)协议的使用步骤:制定协议-->遵守协议-->实现协议 ...
- OC基础1:一些基本概念
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于类方法和实例方法: (1).类方法 ...
- OC基础15:内存管理和自动引用计数
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.什么是ARC? (1).ARC全名为A ...
- oc基础 不可变字符串的创建和使用
oc基础 不可变字符串的创建和使用 简介:下面都是字符串基本用法. 1.字符串的创建 //创建oc常量字符串NSString *str=@"hello world!";NSLog ...
- iOS开发面试知识整理 – OC基础 (二)
iOS | 面试知识整理 – OC基础 (二) 1.C和 OC 如何混编 xcode可以识别一下几种扩展名文件: .m文件,可以编写 OC语言 和 C 语言代码 .cpp: 只能识别C++ 或者C语言 ...
- OC基础学习 调用方式
OC基础学习 调用方式 调用方法: C++里,送一个消息给对象(或者说调用一个方法)的语法如下: obj.method(argument); Objective-C则写成: [obj method: ...
- iOS战记 ----OC基础语法之战(一)
iOS战记 ----OC基础语法之战(一) 一个机械设计者的转行之战. 火车一个年轻的程序员和一个项目经理登上了一列在山里行驶的火车,他们发现列车上几乎都坐满了,只有两个在一起的空位,这个空位的对面 ...
最新文章
- python with上下文管理
- PDF文件修改后,保存时出现:文档无法保存。读取文档时出现问题(135)
- CentOS下搭建Squid代理服务器
- LINQ-from多from
- php中include和require,在PHP中include和require到底有什么区别呢?
- 如何解决push commit conflict
- 【今日CV 计算机视觉论文速览 第148期】Mon, 29 Jul 2019
- MODBUS通讯:libmodbus库使用方法
- 微信跳转浏览器提示_微信跳转外部浏览器跳转提示的2种实现方式
- (14)Python_SimpleImputer缺失值处理
- Atitit 图像处理之编程之类库调用的接口api cli gui ws rest attilax大总结.docx
- 重温JS基础--引用类型(一)
- java初级程序员_为什么现在JAVA初级程序员要求这么高?
- 阿里云虚拟主机备案期间网站调试
- 英语播客列表opml_Hanselminutes播客Feed列表
- 没想到一个起点中文网都可以掀起这么大的波澜
- SPSS异方差检验的实现
- Linux基础-分区规划与使用
- 关于Unity下载资源默认下载到C盘,更改到其他盘的方法
- 【论文解读 | AAAI2020】NeuralCD:Neural Cognitive Diagnosis for Intelligent Education Systems