UITableViewDataSource协议

前言:

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





 

转载于:https://www.cnblogs.com/jukaiit/p/4702797.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值