TableView与CollectionView的区别
发布日期:2022-02-08 18:03:27
浏览次数:39
分类:技术文章
本文共 1730 字,大约阅读时间需要 5 分钟。
1、遵循协议:
UITableView:
UITableViewDataSource,UITableViewDelegate
UIcollectionView:UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
2、注册cell:
tableview可以不注册cell
collectionView必须注册cell,否则会崩溃① UITableView:
用[tableView dequeueReusableCellWithIdentifier:]
方法创建cell可以不用注册cell, 但是需要考虑cell为空的情况,并做相应处理。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"]; } cell.textLabel.text = @"system cell"; return cell;}
如果使用如下代码创建cell,必须注册cell,否则会crash。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
注:如果注册了cell,则 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中不需要处理cell为空的情况。
② UICollectionView:
需要注册cell,不用处理cell为空的情况。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCollectionViewCell" forIndexPath:indexPath];return cell;}
3、CollectionView特定的属性:
UICollectionViewFlowLayout对象
- 有sectionInset属性,控制section的上左下右四个方向距离当前CollectionView的偏移量;
- 有行间距(minimumLineSpacing)和列间距(minimumInteritemSpacing)属性;(itemSize类似于TableView的rowHeight就不赘述了)
开发中可以充分利用sectionInset:
宽为375的屏幕,需要一行显示三个width=100的item,且列间距为0,如何设置?a> 可以初始化collectionView的时候设置宽度为300;b >设置sectionInset = UIEdgeInsetsMake(0, 37.5, 0, 37.5);
4、总结:
创建cell时带forIndexPath:
的方法需要注册cell;
转载地址:https://blog.csdn.net/Evelynzn/article/details/80222269 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月01日 13时00分51秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
用C语言写PHP扩展
2019-04-27
PHP Extension programming
2019-04-27
海量数据处理
2019-04-27
PHP防止注入攻击
2019-04-27
多路IO复用模型 select epoll 等
2019-04-27
Linux Epoll介绍和程序实例
2019-04-27
output_buffering详细介绍
2019-04-27
php缓冲 output_buffering和ob_start
2019-04-27
php error_reporting 详解
2019-04-27
剖析PHP中的输出缓冲
2019-04-27
HTTP响应头不缓存
2019-04-27
PHP安装扩展mcrypt以及相关依赖项 【PHP安装PECL扩展的方法】
2019-04-27
Javascript到PHP加密通讯的简单实现
2019-04-27
德国SNS交友/视频网站Poppen.de的技术架构分享
2019-04-27
UNIX环境编程
2019-04-27
一笔画问题【数据结构-图论】
2019-04-27
红黑树
2019-04-27
安装多个gcc
2019-04-27
Linux0.01内核根目录Makefile注释
2019-04-27
【CSDN2012年度博客之星】需要您的一票,感谢大家的支持
2019-04-27