UICollectionView的概念
UICollectionView是一种新的数据展示方式,简单来说可用把他理解为多列的UITableView.
CollectionView 的结构:
1.cell(单元格)用于展示内容 的主体,不同的cell可用指定不同的尺寸和不同的内容
2.supplementary Views (补充视图)相当于TableView每个section的Head或者Footer,用来标记每个section的view
3.decoration Views (装饰视图)每个section的背景.
UICollectionView的数据源和代理及Layout概念:
DataSource:数据源(为view提供数据源,以及告诉view该显示什么东西及如何显示)
delegate: 代理(提供一些样式及用户的响应)
Layout:布局,ios 有一个自定义的子类:FlowLayout.(布局每个cell的大小及间距等)
它们的关系如下:
UICollectionView的基本用法:
1.初始化单元格方法:
通过注册类加载单元格:
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
通过注册xib加载单元格
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
通过注册类加载单元格(带补充视图):
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
通过注册xib加载单元格(带补充视图)
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
2.UICollectionView的cell的常用方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
2.cell的常用方法:
@required
//每个section的cell(item)的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定义每一个cell(item)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
@optional
//CollectionView中section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
//可移动collectionView中的item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
//可根据索引移动单元格
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
UICollectionView的单元格:
单元格的结构:
● cell本身容器的view
● 一个大小自适应整个cell的backgroundView(平时的背景)
● selectBackgroundView,选中的背景颜色
● contentView,自定义内容应加载到这个视图中
UICollectionView的布局
1.概念与作用:
● 它负责将cell,supplymentary View和decoration View 组织
● 可通过设置该对象Layout来为个视图定义属性
● 通过该对象可以实现不同样式的视图
2.layout对象的创建
简单定义一个FlowLayout对象,可直接使用,如下:
//布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//item的大小
flowLayout.itemSize = CGSizeMake(KScreenWidth-100, self.height - 100);
//行与行之间的间距
flowLayout.minimumLineSpacing = 30;
//集合视图的滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//将 flowLayout设置为UICollectionView的布局
largeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, KScreenWidth, self.height - 50 -50) collectionViewLayout:flowLayout];
UICollectionView布局的常用方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//集合视图的边界值
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
通过这次总结更清晰的了解了集合视图的基本概念及如何使用,但是要更深的学习还得学以致用才能理解透彻,所以有必要多实践一下.
UICollectionView是一种新的数据展示方式,简单来说可用把他理解为多列的UITableView.
CollectionView 的结构:
1.cell(单元格)用于展示内容 的主体,不同的cell可用指定不同的尺寸和不同的内容
2.supplementary Views (补充视图)相当于TableView每个section的Head或者Footer,用来标记每个section的view
3.decoration Views (装饰视图)每个section的背景.
UICollectionView的数据源和代理及Layout概念:
DataSource:数据源(为view提供数据源,以及告诉view该显示什么东西及如何显示)
delegate: 代理(提供一些样式及用户的响应)
Layout:布局,ios 有一个自定义的子类:FlowLayout.(布局每个cell的大小及间距等)
它们的关系如下:
UICollectionView的基本用法:
1.初始化单元格方法:
通过注册类加载单元格:
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
通过注册xib加载单元格
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
通过注册类加载单元格(带补充视图):
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
通过注册xib加载单元格(带补充视图)
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
2.UICollectionView的cell的常用方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
2.cell的常用方法:
@required
//每个section的cell(item)的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定义每一个cell(item)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
@optional
//CollectionView中section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
//可移动collectionView中的item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
//可根据索引移动单元格
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
UICollectionView的单元格:
单元格的结构:
● cell本身容器的view
● 一个大小自适应整个cell的backgroundView(平时的背景)
● selectBackgroundView,选中的背景颜色
● contentView,自定义内容应加载到这个视图中
UICollectionView的布局
1.概念与作用:
● 它负责将cell,supplymentary View和decoration View 组织
● 可通过设置该对象Layout来为个视图定义属性
● 通过该对象可以实现不同样式的视图
2.layout对象的创建
简单定义一个FlowLayout对象,可直接使用,如下:
//布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//item的大小
flowLayout.itemSize = CGSizeMake(KScreenWidth-100, self.height - 100);
//行与行之间的间距
flowLayout.minimumLineSpacing = 30;
//集合视图的滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//将 flowLayout设置为UICollectionView的布局
largeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, KScreenWidth, self.height - 50 -50) collectionViewLayout:flowLayout];
UICollectionView布局的常用方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//集合视图的边界值
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
通过这次总结更清晰的了解了集合视图的基本概念及如何使用,但是要更深的学习还得学以致用才能理解透彻,所以有必要多实践一下.