iOS——UICollectionView
来源:互联网 发布:减肥从130到90 知乎 编辑:程序博客网 时间:2024/06/06 02:13
一、UICollectionView概述
继承与UIScrollView的一个视图,也称为集合视图。
UICollectionView的实现跟tableView不一样的地方在于Item的布局稍微复杂一点,需要用UICollectionViewLayout类来描述视图的布局。我们在项目中常用的是系统提供的UICollectionViewLayout类,也可以自定义UICollectionViewLayout。
二、UICollectionViewLayout
设置每个item的大小
@property (nonatomic) CGSize itemSize;
设置每个item的最小行间距(默认是10)
@property (nonatomic) CGFloat minimumLineSpacing;
设置每个itme的最小列间距(默认是10)
@property (nonatomic) CGFloat minimumInteritemSpacing;
头部引用的尺寸
@property (nonatomic) CGSize headerReferenceSize;
尾部引用的尺寸
@property (nonatomic) CGSize footerReferenceSize;
设置UICollectionView的滑动方向
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;typedef NS_ENUM(NSInteger, UICollectionViewScrollDirection) { UICollectionViewScrollDirectionVertical, //垂直 UICollectionViewScrollDirectionHorizontal //水平};
三、UICollectionView
1.属性
初始化
-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
注册cell
-(void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;-(void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;-(void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;-(void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
重用cell
-(__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;-(__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
刷新collection
-(void)reloadData;
2.代理方法(必须)
返回个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
3.代理方法(可选)
点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
四、返回头部、尾部视图样式
UICollectionView不能像UITableView一样直接指定头部和尾部视 图,需要注册使⽤用,最⼤大的好处是添加了重⽤用机制。
1.注册头部、尾部视图
[self.collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];[self.collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView"];
2.返回头部、尾部视图的样式
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ //需要判断是否返回头部视图还是尾部视图 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //初始化头部视图 UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath]; //设置头部样式 headerView.backgroundColor = [UIColor redColor]; //返回头部视图 return headerView; }else{ if ([kind isEqualToString:UICollectionElementKindSectionFooter]) { //初始化尾部视图 UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView" forIndexPath:indexPath]; //设置尾部样式 footView.backgroundColor = [UIColor yellowColor]; //返回尾部视图 return footView; } } }
0 0
- iOS——UICollectionView
- ios开发——UICollectionView初阶
- UICollectionView——iOS学习连载27
- iOS UICollectionView简介 —— HERO博客
- iOS学习之——UICollectionView详解
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- iOS流布局UICollectionView系列二——UICollectionView的代理方法
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- iOS UICollectionView实用练习 —— HERO博客
- iOS UICollectionView实现图片轮播 —— HERO博客
- iOS开发——新特性界面(UICollectionView)
- iOS学习之—— UICollectionView导致的崩溃问题
- iOS开发之高级视图—— UICollectionView
- IOS UICollectionView
- iOS- UICollectionView
- iOS UICollectionView
- 自学git(一)
- iOS开发的技巧
- mysql的插入数据和查询
- 聚类算法实践(二)——谱聚类、Chameleon聚类
- [HNOI2011]XOR和路径
- iOS——UICollectionView
- 不使用第三个变量来交换连个数据
- Codeforces418D Big Problems for Organizers
- Android版日语学习应用的逆向分析
- C++中头文件的使用
- 一个Linux下的网络模拟工具 Core
- 进程—僵尸进程与孤儿进程
- 基于R语言的模型组合
- Centos 6,7 YUM本地源,网络源