iOS基础 - UITableView的数据源(dataSource)和代理(delegate)

UITableView的数据源(dataSource)和代理(delegate)

UITableView需要一个数据源(dataSource)来显示数据,UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。没有设置数据源的UITableView只是个空壳。凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源。

通常都要为UITableView设置代理对象(delegate),以便在UITableView触发一下事件时做出相应的处理,比如选中了某一行。凡是遵守了UITableViewDelegate协议的OC对象,都可以是UITableView的代理对象。一般会让控制器充当UITableView的dataSource和delegate

UITableViewDataSource

@required

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

第section分区一共有多少行

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

创建第section分区第row行的UITableViewCell对象(indexPath包含了section和row)

@optional

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

一共有多少个分区

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

第section分区的头部标题

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

第section分区的底部标题

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

某一行是否可以编辑(删除)

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

某一行是否可以移动来进行重新排序

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

UITableView右边的索引栏的内容

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

选中了UITableView的某一行

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

某一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

第section分区头部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

第section分区尾部的高度

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

第section分区头部显示的视图

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

第section分区尾部显示的视图

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

设置每一行的等级缩进(数字越小,等级越高)

UITableViewCell

UITableView的每一行都是一个UITableViewCell,通过dataSource的

tableView:cellForRowAtIndexPath:方法来初始化每一行

UITableViewCell是UIView的子类,内部有个默认的子视图:contentView。contentView是UITableViewCell所显示内容的父视图,并负责显示一些辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

UITableViewCell的contentView

contentView下默认有3个子视图,其中的2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问),第3个是UIImage

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值