您的位置:首页 > 产品设计 > UI/UE

UITableView中的DataSource和delegate

2016-04-22 11:00 507 查看
1,顾名思义,dataSource意思为数据源,delegate意为代理,其内部包含很多方法。

UITableView需要显示数据,则数据源(datasource)可以给他提供数据从而显示出来,其会向数据源查询一共有多少行数据以及显示什么数据等,如果没有设置数据源的,那么UITableView就不会产生任何数据,没有作用。同时,遵守UITableViewDataSource协议的都可以是数据源,所以添加协议从而提供数据源。

同时UITableView也要设置对象,以防在UITableView触发事件时可以做出处理,比如选中某一行和某一列。遵守UITableViewDataSource协议的都可以是代理对象。

2,上边提到的UITableViewDataSource 协议中常用方法:

(1.)设置右边索引值 

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

(2.)设置分组标识

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

(3.)设置分组个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

(4.)设置行数 

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

(5.)创建cell(使用重用机制,如下例)

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

(6.)设置tableView的每一行的编辑状态(YES,可编辑)

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

{

return YES

}

(7.)edit按钮的点击事件(当点击edit按钮时触发) 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

(8.)当提交编辑操作时触发 

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

(9.)设置tableView每一行是否允许移动(YES,可移动) 

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

{

return YES

}

(10.)提交移动操作之后触发 

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

3,UITableViewDelegate协议中常用方法

(1).设置行高 

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

{

return 55;

}

(2).选中cell时触发 

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

(3.)设置tableViewCell的编辑样式(插入/删除) 

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

(4.)设置当点击编辑按钮时上面显示的文字,如显示删除 

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) { return
@"删除"; }

(5).设置cell移动的位置 

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: