数据源方法和委托方法
2015-08-21 20:32
441 查看
数据源方法和委托方法
表视图的继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动,它的父类,我们将在后面的课程中再次提及
数据源方法(UITableViewDatasource): 实例化表视图时,必须要实现它的数据源方法,以此来完成表中数据的配置(一般来说数据源方法是用来配置表中的数据)
委托方法(UITableViewDelegate): 表视图的委托方法(代理方法),一般是处理表视图基本样式(单元格的高度)以及捕捉选中单元格选中事件等
表示视图调用顺序—委托方法
创建和配置表视图的顺序
创建表视图实例,初始化风格和大小
设置数据源方法和委托方法 开始调用数据源方法,(注意事件循环没有结束)
调用顺序:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 1,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 3
常用数据源方法和委托方法
常用数据源方法
// 配置section中含有行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 创建单元格实例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
// 配置表视图section个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// section中的头部视图的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// section中的尾部视图的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
/* 表视图的编辑 移动、删除等 */
// 指定单元格是否支持编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
// 指定单元格是否支持移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
// 用户编辑了哪一个单元格,在这里执行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath*)indexPath;
// 实现此方法,移动单元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
常用委托方法
// 配置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置section 头部、尾部视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// 自定义section头部、尾部视图,注意:需要指定高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
// 用户单击单元格中辅助按钮时,调用该方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
// 用户单击单元格,调用该方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// 取消单元格时,调用该方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// 设置单元格编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
表视图的继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动,它的父类,我们将在后面的课程中再次提及
数据源方法(UITableViewDatasource): 实例化表视图时,必须要实现它的数据源方法,以此来完成表中数据的配置(一般来说数据源方法是用来配置表中的数据)
委托方法(UITableViewDelegate): 表视图的委托方法(代理方法),一般是处理表视图基本样式(单元格的高度)以及捕捉选中单元格选中事件等
表示视图调用顺序—委托方法
创建和配置表视图的顺序
创建表视图实例,初始化风格和大小
设置数据源方法和委托方法 开始调用数据源方法,(注意事件循环没有结束)
调用顺序:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 1,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 3
常用数据源方法和委托方法
常用数据源方法
// 配置section中含有行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 创建单元格实例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
// 配置表视图section个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// section中的头部视图的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// section中的尾部视图的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
/* 表视图的编辑 移动、删除等 */
// 指定单元格是否支持编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
// 指定单元格是否支持移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
// 用户编辑了哪一个单元格,在这里执行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath*)indexPath;
// 实现此方法,移动单元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
常用委托方法
// 配置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置section 头部、尾部视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// 自定义section头部、尾部视图,注意:需要指定高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
// 用户单击单元格中辅助按钮时,调用该方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
// 用户单击单元格,调用该方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// 取消单元格时,调用该方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// 设置单元格编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
相关文章推荐
- metaq spring配置
- 深入理解JavaScript系列(42):设计模式之原型模式
- 2899 Strange fuction【二分+数学】
- 九度OJ 题目1076:N的阶乘
- 操作系统常见面试题总结
- CodeForce 569A
- 深入理解JavaScript系列(41):设计模式之模板方法
- UIView 中常见的方法总结
- UIBezierPath
- Hibernate 如果 hibernate.cfg.xml 没有放在 src 下,则使用以下配置方式
- 思路题 codeforces div2 C Replacement
- hdu 2544 最短路(Dijkstra Or Floyd Or dfs)
- Spring EL hello world example
- Hibernate Hibernate配置信息+启动session工厂工具包
- 算法题:矩阵旋转90度
- Java中常见概念
- c/c++常见关键字
- 表视图常用属性和方法
- CodeForces 478B 第六周比赛B题
- 导航控制器的基本概念