分组样式顾名思义是对TableView中的数据行进行分组处理,每一个分组都有一个header和footer。spa
TableView中header的英文文本是大写的,footer的英文文本是小写的。以下图浅灰色区域就是header和footer。3d
header的做用更像是标题,而footer则是详细描述信息code
在以前的文章中咱们建立的TableView样式是UITableViewStylePlain,分组样式能够使用UITableViewStyleGrouped建立:blog
设置UITableView的header和footer的文本it
UITableViewDataSource中有两个方法能够帮助咱们设置header和footer的本文,它们分别是:io
- (NSString *)tableView:(UITableView *
)tableView titleForHeaderInSection:(NSInteger)sectiontable
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)sectionclass
下面咱们实现这两个方法:方法
运行效果:im
自定义Header和Footer的样式(view)
经过下面几个在UITableViewDelegate协议中声明的方法,咱们能够对Header和Footer进行自定义操做。
- (UIView *)tableView:() viewForHeaderInSection:()UITableView *tableViewNSIntegersection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (UIView *)tableView:() viewForFooterInSection:()UITableView *tableViewNSIntegersection
- (CGFloat)tableView:() heightForFooterInSection:()UITableView *tableViewNSIntegersection
方法实现:
做为演示,只是简单设置了一个UIView做为header和footer的视图,
运行效果: