您的位置:首页 > 其它

数据源方法和委托方法

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: