没有合适的资源?快使用搜索试试~ 我知道了~
首页UICollectionView、UICollectionView基本用法、UICollectionView详解
UICollectionView、UICollectionView基本用法、UICollectionView详解
需积分: 50 8 下载量 200 浏览量
更新于2023-05-24
收藏 97KB DOCX 举报
UICollectionView、UICollectionView基本用法、UICollectionView详解,UICollectionView常用方法
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/10474700/bg1.jpg)
前言
这几天有时间看了下 UICollectionView 的东西,才发觉它真的非常强大,很有
必要好好学习学习。以前虽然用过几次,但没有系统的整理总结过。这两天我
为 UICollectionView 做一个比较全面的整理。包括基本使用,自定义布局,自
定义插入删除动画,自定义转场动画等几部分。好了,开始。
UICollectionView 相对于 UITableView 可以说是青出于蓝而胜于蓝,它和
UITableView 很相似,但它要更加强大。
UITableView 的布局形式比较单一,局限于行列表,而 UICollectionView 的强大
之处在于把视图布局分离出来成为一个独立的类,你想实现怎样的视图布局,
就子类化这个类并在其中实现。
UICollectionView 基础
UICollectionViewFlowLayout:视图布局对象(流视图:一行排满,
自动排到下行),继承自 UICollectionViewLayout。
UICollectionViewLayout 有个 collectionView 属性,
所有的视图布局对象都继承自 UICollectionViewLayout。若我们要自
定义布局对象,我们一般继承 UICollectionViewFlowLayout 就可以了。
需要实现三个协议;UICollectionViewDataSource(数据源)、
UICollectionViewDelegateFlowLayout(视图布局)、
UICollectionViewDelegate。
可以看得出,除了视图布局,UICollectionView 几乎和 UITableView
一样,但这也正是它的强大之处。
![](https://csdnimg.cn/release/download_crawler_static/10474700/bg2.jpg)
1.创建 UICollectionView 视图
- (void)loadCollectionView
{
_customLayout = [[CustomCollectionViewLayout alloc] init]; // 自
定义的布局对象
_collectionView = [[UICollectionView alloc]
initWithFrame:self.view.bounds collectionViewLayout:_customLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[self.view addSubview:_collectionView];
// 注册 cell、sectionHeader、sectionFooter
[_collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:cellId];
[_collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:headerId];
[_collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:footerId];
}
需要注意的是这几行代码的位置,及 const 的位置。(我经常搞乱)
@implementation YWViewController
// 注意 const 的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";
- (void)viewDidLoad
{
2.实现 UICollectionViewDataSource 的几个代理方法
![](https://csdnimg.cn/release/download_crawler_static/10474700/bg3.jpg)
#pragma mark ---- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return _section0Array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [_collectionView
dequeueReusableCellWithReuseIdentifier:cellId
forIndexPath:indexPath];
cell.backgroundColor = [UIColor purpleColor];
return cell;
}
// 和 UITableView 类似,UICollectionView 也可设置段头段尾
- (UICollectionReusableView *)collectionView:(UICollectionView
*)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
if([kind isEqualToString:UICollectionElementKindSectionHeader])
{
UICollectionReusableView *headerView = [_collectionView
dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:headerId forIndexPath:indexPath];
if(headerView == nil)
{
headerView = [[UICollectionReusableView alloc] init];
}
headerView.backgroundColor = [UIColor grayColor];
剩余10页未读,继续阅读
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
qq_41976042
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- BSC关键绩效财务与客户指标详解
- 绘制企业战略地图:从财务到客户价值的六步法
- BSC关键绩效指标详解:财务与运营效率评估
- 手持移动数据终端:常见问题与WIFI设置指南
- 平衡计分卡(BSC):绩效管理与战略实施工具
- ESP8266智能家居控制系统设计与实现
- ESP8266在智能家居中的应用——网络家电控制系统
- BSC:平衡计分卡在绩效管理与信息技术中的应用
- 手持移动数据终端:常见问题与解决办法
- BSC模板:四大领域关键绩效指标详解(财务、客户、运营与成长)
- BSC:从绩效考核到计算机网络的关键概念
- BSC模板:四大维度关键绩效指标详解与预算达成分析
- 平衡计分卡(BSC):绩效考核与战略实施工具
- K-means聚类算法详解及其优缺点
- 平衡计分卡(BSC):从绩效考核到战略实施
- BSC:平衡计分卡与计算机网络中的应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)