iOS UITableView代理方法详解

简介: iOS UITableView代理方法详解

iOS UITableView的代理方法详解


一、补充


在上一篇博客中,http://my.oschina.net/u/2340880/blog/404605,我将IOS中tableView(表视图)的一些常用方法总结了一下,这篇将tableView的代理方法作了总结,对上一篇博客进行了补充。


二、UITableViewDataSourc(数据源代理)


1、必须实现的回调方法


返回每个分区的行数



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



返回每一行的cell



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



2、可选实现的方法


返回分区数(默认为1)



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


返回每个分区头部的标题



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


返回每个分区的尾部标题



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


设置某行是否可编辑



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


设置某行是否可以被移动



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


设置索引栏标题数组(实现这个方法,会在tableView右边显示每个分区的索引)



- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;


设置索引栏标题对应的分区



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



tableView接受编辑时调用的方法



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


这个方法中的editingStyle参数是一个枚举,代表了cell被编辑的模式,如下:


typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

   UITableViewCellEditingStyleNone,//没有编辑操作

   UITableViewCellEditingStyleDelete,//删除操作

   UITableViewCellEditingStyleInsert//插入操作

};


tableView的cell被移动时调用的方法



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



三、UITableViewDelegate(tableView代理)


cell将要显示时调用的方法



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


头视图将要显示时调用的方法



- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section;


尾视图将要显示时调用的方法



- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;



和上面的方法对应,这三个方法分别是cell,头视图,尾视图已经显示时调用的方法



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


- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section;


- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section;



设置行高,头视图高度和尾视图高度的方法



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


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;


设置行高,头视图高度和尾视图高度的估计值(对于高度可变的情况下,提高效率)



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


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;



设置自定义头视图和尾视图



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;



设置cell是否可以高亮



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



cell高亮和取消高亮时分别调用的函数



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


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



当即将选中某行和取消选中某行时调用的函数,返回一直位置,执行选中或者取消选中



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


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



已经选中和已经取消选中后调用的函数



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


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



设置tableView被编辑时的状态风格,如果不设置,默认都是删除风格



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



自定义删除按钮的标题



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



下面这个方法是IOS8中的新方法,用于自定义创建tableView被编辑时右边的按钮,按钮类型为UITableViewRowAction。



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



设置编辑时背景是否缩进



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



将要编辑和结束编辑时调用的方法



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


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



移动特定的某行



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


 

目录
相关文章
|
移动开发 前端开发 数据安全/隐私保护
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
1184 0
|
运维 iOS开发 Windows
windows电脑备案ios APP获取公钥和证书指纹Sha-1值的方法
在阿里云进行APP备案、在备案IOS端的环节的时候,发现需要我们将p12证书安装在电脑上,再用xcode或或钥匙串访问来获取这个证书的公钥和sha-1值。 但是大部分开发uniapp应用的同学们,或者进行发布的运维人员的电脑都是windows,无法按照阿里云的教程来获取ios的公钥和sha-1。备案就被卡主了。 这里介绍下另一个方法,就是使用香蕉云编来在线上传证书获取。如下图所示,打开香蕉云编后,找到下图这个功能
1750 0
|
语音技术 开发工具 图形学
Unity与IOS⭐一、百度语音IOS版Demo调试方法
Unity与IOS⭐一、百度语音IOS版Demo调试方法
|
Android开发 iOS开发 开发者
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
2079 0
|
iOS开发 开发者 Windows
uniapp云打包ios应用证书的获取方法,生成指南
打包用到的一共两个文件,一个是p12格式的私钥证书,一个是证书profile文件。其中生成p12证书的时候,按照官网的教程,是需要MAC电脑来协助做的,主要是生成一些csr文件和导出p12证书等。其实这些步骤也可以借助一些其他的工具来实现,不一定使用mac电脑,用windows电脑也可以创建。
1839 0
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
685 2
|
iOS开发
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
3927 0
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
|
安全 编译器 开发工具
iOS 安全加固方法及实现
iOS 安全加固方法及实现
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
631 0
|
安全 编译器 开发工具
​iOS安全加固方法及实现
​iOS安全加固方法及实现
284 0

热门文章

最新文章