络腮胡菲菲 发表于 2024-11-13 21:49:53

iOS开辟- tableView的协议

在利用 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企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: iOS开辟- tableView的协议