Tableview自定义分组头,分组尾

来源:互联网 发布:linux设置为中文后死机 编辑:程序博客网 时间:2024/05/07 14:25

自定义组头组尾,可以想到需要使用下面的代理方法:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height

所以我们需要自定义view。但是uiview不支持带XIB,如何实现?这里有一个技巧。我们创建一个uiview叫做myview。然后创建一个空的xib.里面拖拽出你想要的界面。然后将他们绑定
如下图:
这里写图片描述
这里写图片描述
自定义view就做好了,名字叫做myview。下面把它放到tableview中

//设置分区头的高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    return 100;}//设置分区头视图  (自定义分区头 一定要设置分区头高度)-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {    UIView *headerview = [[NSBundle mainBundle]loadNibNamed:@"myview" owner:self options:nil].firstObject;    headerview.backgroundColor = [UIColor grayColor];    return headerview;}-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 100;}-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    UIView *footerview = [[NSBundle mainBundle]loadNibNamed:@"myview" owner:self options:nil].firstObject;    footerview.backgroundColor = [UIColor yellowColor];    return footerview;}

实现效果如下:
这里写图片描述