·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> UITableViewDataSource协议

UITableViewDataSource协议

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

前言:

在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协议的方法;