UITableViewDataSource协议

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

前言:

在iOS开发中,表视图UITableView 是我们做UI界面设计时的重要视图。

那么,使用表视图UITableView 需要遵守哪些协议呢?

<UITableViewDataSource,UITableViewDelegate>

UITableViewDataSource 表视图数据源协议,用来控制表视图的显示内容;

UITableViewDelegate 表视图协议,用来控制表视图的显示以及每个cell的高度和每个分区的头尾高度等;

本文先学习UITableViewDataSource协议。

UITableViewDataSource协议有哪些方法呢?

首先,必须要实现的方法:

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

这个方法用来设置tableView的每个分组的行数;

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

这个方法用来返回cell,就是用来控制每一行返回的内容;

以上两个方法是必须实现的;

其他可选择的呢?

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

设置tableView里面有几个分区,如果不写默认为1;

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

设置分区头的内容,返回值是NSString类型;

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

同理,这个方法是设置分区尾的内容,返回值是NSString类型;

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

这个方法是用来控制Cell是否可以编辑,如果没有实现,所有行被认为是可编辑的;

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

这个方法是用来控制Cell是否可以移动,只有实现了,才可以移动;

- (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;

在编辑结束cell的时候,保存修改;

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

根据行数交换cell;

以上就是所有的UITableViewDataSource协议的方法;

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航