iOS文档补完计划--UITableViewDataSource&&UITableViewDelegate

目录主要分为以下几个样式:
常用、会用、了解

目录

  • UITableViewDataSource
  • 配置TableView
    • tableView:cellForRowAtIndexPath:
    • tableView:numberOfRowsInSection:
    • numberOfSectionsInTableView:
    • tableView:titleForHeaderInSection:
    • tableView:titleForFooterInSection:
  • 插入&&删除
    • tableView:commitEditingStyle:forRowAtIndexPath:
    • tableView:canEditRowAtIndexPath:
  • 重新排序
    • tableView:canMoveRowAtIndexPath:
    • tableView:moveRowAtIndexPath:toIndexPath:
  • 索引设置
    • sectionIndexTitlesForTableView:
    • tableView:sectionForSectionIndexTitle:atIndex:
  • UITableViewDelegate
  • 配置Row
    • tableView:heightForRowAtIndexPath:
    • tableView:estimatedHeightForRowAtIndexPath:
    • tableView:indentationLevelForRowAtIndexPath:
    • tableView:willDisplayCell:forRowAtIndexPath:
    • tableView:shouldSpringLoadRowAtIndexPath:withContext:
  • 扩展按钮
    • tableView:editActionsForRowAtIndexPath:
    • tableView:accessoryButtonTappedForRowWithIndexPath:
  • 选择管理
    • tableView:willSelectRowAtIndexPath:
    • tableView:didSelectRowAtIndexPath:
    • tableView:willDeselectRowAtIndexPath:
    • tableView:didDeselectRowAtIndexPath:
  • 页眉和页脚
    iOS11需要注意的新特性
  • 编辑表单(左滑)
    • tableView:willBeginEditingRowAtIndexPath:
    • tableView:didEndEditingRowAtIndexPath:
    • tableView:editingStyleForRowAtIndexPath:
    • tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
    • tableView:shouldIndentWhileEditingRowAtIndexPath:
  • 重新排序表单
    • tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
  • 移动出可见范围
    • cell/sectionHeaderView/sectionFooterView的消失
  • MENU菜单
  • 高亮管理

UITableViewDataSource

一个能够为UITableView提供展示要素的代理


配置TableView

  • tableView:cellForRowAtIndexPath:

将返回的Cell插入TableView的indexPath位置。(一旦需要展示cell)则必须实现且不能返回nil。

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath;

返回的对象通常是重用的cell。

  • - tableView:numberOfRowsInSection:

返回该节有多少行内容(必须实现

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section;
  • numberOfSectionsInTableView:

返回一共有多少节内容需要展示

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

默认返回1

  • - tableView:titleForHeaderInSection:
  • - tableView:titleForFooterInSection:

为系统默认样式的sectionHeader/sectionFooter设置文字

- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section;

除了敲demo通常没啥用。你应该使用tableView:viewForHeaderInSection:进行自定义样式。


插入&&删除

  • - tableView:commitEditingStyle:forRowAtIndexPath:

编辑完成时调用

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath;

如果点击其他地方被取消、则不会被调用。

editingStyle是一个枚举、有三种可能的值

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
    UITableViewCellEditingStyleNone,//没有编辑样式
    UITableViewCellEditingStyleDelete,//删除样式 (左边是红色减号)
    UITableViewCellEditingStyleInsert//插入样式  (左边是绿色加号)
};

当你进行了对应的操作(点击了绿色的添加或者红色的删除)、代理将会告知你并让你修改数据源以配合操作。

  • - tableView:canEditRowAtIndexPath:

返回该位置的cell是否可以进入编辑状态

- (BOOL)tableView:(UITableView *)tableView 
canEditRowAtIndexPath:(NSIndexPath *)indexPath;

如果没有实现此方法、那么默认所有的cell都可以进入编辑状态。
这个编辑状态有两种意思:

  1. 通过[self.tableView setEditing:YES animated:YES];让所有cell进入
  2. 右滑

重新排序

让tableView支持拖动、需要以下条件

  1. 让tableView进入编辑状态
    也就是设置它的editing为YES

  2. 返回编辑模式
    也就是实现UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回UITableViewCellEditingStyleNone模式。如果不实现,默认返回的就是删除模式

3、实现tableView:moveRowAtIndexPath:toIndexPath方法
只要实现该方法,就能实现单元格的拖动排序,但只是实现了表面的排序,并没有修改真实地数据

4、在方法中完成数据模型的更新

  • tableView:canMoveRowAtIndexPath:

是否可以将该cell拖动到指定位

- (BOOL)tableView:(UITableView *)tableView 
canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

返回该单元格、是否可以被拖动。默认返回YES。

  • - tableView:moveRowAtIndexPath:toIndexPath:

拖动完成时触发、让用户修改数据源

- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
      toIndexPath:(NSIndexPath *)destinationIndexPath;

如果不做修改、cell就仅仅是在当前展示上修改了位置。下次被提取或者刷新时会变回原来的顺序。


索引设置

  • - sectionIndexTitlesForTableView:

返回索引数组

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
  1. tablbView的style必须是UITableViewStylePlain
  2. 返回的数组将会悬浮在tableView右侧
  • - tableView:sectionForSectionIndexTitle:atIndex:

点击索引后、将talbeView移动至哪个section处

- (NSInteger)tableView:(UITableView *)tableView 
sectionForSectionIndexTitle:(NSString *)title 
               atIndex:(NSInteger)index;

支持数组越界


UITableViewDelegate

对页眉页脚、高度、选择、删除等操作进行支持。


配置Row

  • - tableView:heightForRowAtIndexPath:

可以为指定的行设置高度

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath;

你可以rowHeight为所有的cell统一设置高度、相对性能较高。
而一旦实现heightForRowAtIndexPath方法、你就必须为每一个indexPath设置高度。
返回UITableViewAutomaticDimension则由系统自适应计算。

  • - tableView:estimatedHeightForRowAtIndexPath:

返回指定位置的预估行高

- (CGFloat)tableView:(UITableView *)tableView 
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
  1. 如果没有预估、可以返回UITableViewAutomaticDimension
  2. 使用此功能可以将某些计算、从加载时移动到滚动时进行。以提高性能。
  • - tableView:indentationLevelForRowAtIndexPath:

返回缩进级别

- (NSInteger)tableView:(UITableView *)tableView 
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;

并不知道有什么地方必须这样用


  • - tableView:willDisplayCell:forRowAtIndexPath:

某个indexPath的cell将要被绘制(展示)

- (void)tableView:(UITableView *)tableView 
  willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath;

由于cellForRow实际上会预创建一些cell、所以cell内部的赋值可以放在这里、以提升一些性能。
但是最显著的用途还是对刚展示出来的cell做一些动画吧。比如从右侧一个一个滑入之类。

  • - tableView:shouldSpringLoadRowAtIndexPath:withContext:

cell是否支持iOS11 新特性 Drag and Drop (默认YES)

- (BOOL)tableView:(UITableView *)tableView 
shouldSpringLoadRowAtIndexPath:(NSIndexPath *)indexPath 
      withContext:(id<UISpringLoadedInteractionContext>)context;

扩展按钮

  • - tableView:editActionsForRowAtIndexPath:

自定义左滑事件

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView 
                  editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;

返回一个由UITableViewRowAction组成的数组。
他们会响应用对应的点击交互。
如果你不实现这个代理、右滑时将会展示默认按钮。

  • - tableView:accessoryButtonTappedForRowWithIndexPath:

当cell的扩展视图被点击时调用

- (void)tableView:(UITableView *)tableView 
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

这里的扩展视图指的是cell.accessoryType的视图。如下图所示:


选择管理

cell被点击时引起的一系列方法调用

需要注意的是即使cell.selectionStyle = UITableViewCellSelectionStyleNone;使得cell跟随点击而改变背景色、以下方法也会被调用。

  • - tableView:willSelectRowAtIndexPath:

将要被选定时触发

- (NSIndexPath *)tableView:(UITableView *)tableView 
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

允许你通过返回一个新的NSIndexPath、来修改将要被选择的cell。
编辑状态下不会触发此方法

  • - tableView:didSelectRowAtIndexPath:

cell被选定时触发

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

编辑状态下不会触发此方法

  • - tableView:willDeselectRowAtIndexPath:

选中状态被取消时触发

- (NSIndexPath *)tableView:(UITableView *)tableView 
willDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

如果你返回nil、将不会被取消。允许从定向

  • - tableView:didDeselectRowAtIndexPath:

cell选中被取消时触发

- (void)tableView:(UITableView *)tableView 
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

页眉和页脚

//返回页眉和页脚
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; 

//返回页眉和页脚的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

//页眉和页脚将要展示
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

//预估页眉和页脚高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

以上的方法通过方法名基本都能理解其用处。

这里需要注意

iOS11之后、系统为我们设置了预估行高。如果只实现了header高度为0.1f、而没有返回具体的view会引起留白的bug。《详见》


编辑表单

要让表单支持左滑

你必须实现commitEditingStyle方法、以告诉tableView左滑操作是由价值的

  • - tableView:willBeginEditingRowAtIndexPath:

表单即将进入编辑模式(左滑)

- (void)tableView:(UITableView *)tableView 
willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
  • -tableView:didEndEditingRowAtIndexPath:

已经离开编辑模式

- (void)tableView:(UITableView *)tableView 
didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;

编辑接触(并不限于取消)触发、并不会记录你执行了哪种操作。
所以、你应该在tableView:commitEditingStyle:forRowAtIndexPath:而不是这里实现相关编辑操作。

  • - tableView:editingStyleForRowAtIndexPath:

决定左滑返回哪种附加视图

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

默认返回UITableViewCellEditingStyleDelete

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
    UITableViewCellEditingStyleNone,//没有编辑样式
    UITableViewCellEditingStyleDelete,//删除样式 (左边是红色减号)
    UITableViewCellEditingStyleInsert//插入样式  (左边是绿色加号)
};
  • - tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

更改UITableViewCellEditingStyleDelete下的文字

- (NSString *)tableView:(UITableView *)tableView 
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;

setEditing以及左滑时同样有效。

  • - tableView:shouldIndentWhileEditingRowAtIndexPath:

进入编辑模式时、cell背景是否缩进

- (BOOL)tableView:(UITableView *)tableView 
shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;

通知委托在编辑模式下是否需要对表视图指定行进行缩进,NO为关闭缩进,这个方法可以用来去掉move时row前面的空白。


重新排序表单

  • tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

在移动cell的时候会多次调用

- (NSIndexPath *)tableView:(UITableView *)tableView 
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;

cell的初始位置以及目标。允许对目标位置重定向。
移动中的留白动画、以及结果都会受此影响。


移动出可见范围

cell/sectionHeaderView/sectionFooterView的消失

//cell已经移动出可见范围
- (void)tableView:(UITableView *)tableView 
didEndDisplayingCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath;

//sectionHeaderView已经移动出可见范围
- (void)tableView:(UITableView *)tableView 
didEndDisplayingHeaderView:(UIView *)view 
       forSection:(NSInteger)section;

//sectionFooterView已经移动出可见范围
- (void)tableView:(UITableView *)tableView 
didEndDisplayingFooterView:(UIView *)view 
       forSection:(NSInteger)section;

MENU菜单

如果想让cell支持menu菜单的呼出、必须将以下三个方法全部实现

  • - tableView:shouldShowMenuForRowAtIndexPath:

长按后是否显示编辑菜单

- (BOOL)tableView:(UITableView *)tableView 
shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath;
  • - tableView:canPerformAction:forRowAtIndexPath:withSender:

返回该cell支持那种action

- (BOOL)tableView:(UITableView *)tableView 
 canPerformAction:(SEL)action 
forRowAtIndexPath:(NSIndexPath *)indexPath 
       withSender:(id)sender;

这个规则你可以参考iOS文档补完计划--UIResponder中关于canPerformAction:withSender:方法的解释。

  • - tableView:performAction:forRowAtIndexPath:withSender:

让代理者对menu操作进行响应

- (void)tableView:(UITableView *)tableView 
    performAction:(SEL)action 
forRowAtIndexPath:(NSIndexPath *)indexPath 
       withSender:(id)sender;

高亮管理

高亮权限、高亮、取消高领

//点击时是否高亮。默认YES
- (BOOL)tableView:(UITableView *)tableView 
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath;

//已经高亮
- (void)tableView:(UITableView *)tableView 
didHighlightRowAtIndexPath:(NSIndexPath *)indexPath;


//不再高亮
- (void)tableView:(UITableView *)tableView 
didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath;

最后

本文主要是自己的学习与总结。如果文内存在纰漏、万望留言斧正。如果愿意补充以及不吝赐教小弟会更加感激。


参考资料

官方文档-UITableView
iOS UITableView 的 Plain和Grouped样式的区别
iOS_UITableView 编辑(cell的插入, 删除, 移动)
ios tableView那些事 (五) 给tableview设置缩进级别
关于UITableView委托方法的功能(by atany)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269