论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
IT评测·应用市场-qidao123.com技术社区
»
论坛
›
软件与程序人生
›
移动端开发
›
IOS
›
iOS开辟- tableView的协议
iOS开辟- tableView的协议
络腮胡菲菲
论坛元老
|
2024-11-13 21:49:53
|
显示全部楼层
|
阅读模式
楼主
主题
1784
|
帖子
1784
|
积分
5352
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
在利用 UITableView 时,必须实现的协议主要包括以下几个
1.
UITableViewDataSource 协议
这是最重要的协议,用于提供数据给 UITableView。没有这个协议,UITableView 是无法表现任何内容的。
必须实现的方法:
tableView:numberOfRowsInSection:
:返回给定 section 中的行数。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
复制代码
tableView:cellForRowAtIndexPath:
:返回对应 indexPath 的单元格(UITableViewCell)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
复制代码
这两个方法是 UITableViewDataSource 协议中最核心的必须实现的方法。
可选的方法:
tableView:titleForHeaderInSection:
:返回指定 section 的标题(用于表头)。
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
复制代码
tableView:titleForFooterInSection:
:返回指定 section 的标题(用于表尾)。
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
复制代码
tableView:canEditRowAtIndexPath:
:指示是否答应编辑某一行。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
复制代码
tableView:canMoveRowAtIndexPath:
:指示是否答应移动某一行。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
复制代码
2.
UITableViewDelegate 协议
UITableViewDelegate 协议用于处理表视图的交互,例如行选择、行删除、行移动等。这个协议的实现通常是为了增强用户体验。
必须实现的方法:
实际上,UITableViewDelegate 中并没有严格“必须”实现的方法,但是通常会实现以下几种常见方法:
tableView:didSelectRowAtIndexPath:
:当用户点击某一行时调用。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
复制代码
可选的方法:
tableView:heightForRowAtIndexPath:
:设置行高。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
复制代码
tableView:heightForHeaderInSection:
:设置表头的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
复制代码
tableView:heightForFooterInSection:
:设置表尾的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
复制代码
tableView:viewForHeaderInSection:
:自界说表头视图。
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
复制代码
tableView:viewForFooterInSection:
:自界说表尾视图。
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
复制代码
tableView:didDeselectRowAtIndexPath:
:当用户取消选择某一行时调用。
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
复制代码
3.
UITableViewDragDelegate 和 UITableViewDropDelegate(iOS 11 及以上)
这些协议主要用于拖放操作(drag and drop)功能,实用于必要支持拖动排序或拖拽添加数据的表格。
UITableViewDragDelegate
:用于处理行拖拽操作。
UITableViewDropDelegate
:用于处理行的接收(drop)操作。
这些协议方法在利用拖放功能时非常有用,但它们是可选的,只在支持拖放操作时才必要实现。
4.
UITableViewDataSourcePrefetching(iOS 10 及以上)
如果表格必要进行数据预加载,UITableViewDataSourcePrefetching 协议非常有用。这个协议答应提前加载即将表现的行的数据(例如,提前加载图片或长途数据)。
tableView:prefetchRowsAtIndexPaths:
:预加载数据的方法。
- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
复制代码
tableView:cancelPrefetchingForRowsAtIndexPaths:
:取消预加载的数据的方法。
- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
复制代码
总结
必需的协议
:
UITableViewDataSource:主要负责提供数据。
UITableViewDelegate:主要负责处理交互(例如行的选择、编辑、行高等)。
可选的协议
:
UITableViewDragDelegate 和 UITableViewDropDelegate(用于拖放操作)。
UITableViewDataSourcePrefetching(用于数据预加载)。
大部分时候,只必要实现 UITableViewDataSource 和 UITableViewDelegate 中的几个关键方法。如果还必要自界说其他功能(例如拖放、数据预加载),可以根据需求再实现其他协议的方法。
而利用UIcollectionView也是类似的。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复
使用道具
举报
0 个回复
正序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
发新帖
回复
络腮胡菲菲
论坛元老
这个人很懒什么都没写!
楼主热帖
论销售、售前的互助与博弈
C++读写文件
B站狂神Docker学习笔记
Python:灵活的开发环境
SPSS计算极值、平均值、中位数、方差、 ...
微信分享iOS Universal Link配置说明 ...
AAA
<C++>继承的进阶之构造与析构的调用 ...
.NET WebAPI 使用 GroupName 对 Contro ...
BP神经网络(反向传播算法原理、推导过 ...
标签云
AI
运维
CIO
存储
服务器
浏览过的版块
网络安全
快速回复
返回顶部
返回列表