iOS tableView设置Header(组头)&Footer(组尾)

前言:
首先,本文展示的是较简单的创建方式,header和footer的视图皆可自定义。

直接看效果图吧:
customView.gif

设置Header:

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

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

设置Footer:

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

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

没错,就是这么几个代理方法

主要代码部分(真的很简单)

#pragma mark - 自定义headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, HeaderHeight)];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HeaderHeight)];
    lab.text = [NSString stringWithFormat:@"----我是%@header呀---",self.sectionArray[section]];
    lab.textAlignment = NSTextAlignmentCenter;
    [header addSubview:lab];
    header.backgroundColor = [UIColor blueColor];
    
    return header;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return HeaderHeight;
}
#pragma mark - 自定义footerView
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return FooterHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, FooterHeight)];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FooterHeight)];
    lab.text = [NSString stringWithFormat:@"----我是%@footer呀---",self.sectionArray[section]];
    lab.textAlignment = NSTextAlignmentCenter;
    [footer addSubview:lab];
    footer.backgroundColor = [UIColor brownColor];
    
    return footer;
}

全部代码部分:

#import "JHCustomTableVC.h"

#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface JHCustomTableVC ()<UITableViewDelegate,UITableViewDataSource>
/// tableView
@property (strong, nonatomic) UITableView *tableView;
/** section*/
@property (nonatomic, strong) NSMutableArray * sectionArray;
/** row*/
@property (nonatomic, strong) NSMutableArray * rowArray;
@end

static NSString *cellID = @"JHCustomTableVCCell";
static CGFloat FooterHeight = 30;
static CGFloat HeaderHeight = 60;

@implementation JHCustomTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self getData];
}

- (void)getData{
    _sectionArray = [[NSMutableArray alloc]init];
    [_sectionArray addObjectsFromArray:@[@"第一组",@"第二组",@"第三组"]];
    
    _rowArray = [[NSMutableArray alloc]init];
    [_rowArray addObjectsFromArray:@[@"第一行",@"第二行",@"第三行"]];
    
    [self.tableView reloadData];
}
- (void)setupUI{
    
    self.title = @"header & footer";
    self.view.backgroundColor = [UIColor colorWithRed:242/250.0 green:243/250.0 blue:240/250.0 alpha:1];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-44) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithRed:242/250.0 green:243/250.0 blue:240/250.0 alpha:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.rowArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.rowArray[indexPath.row];
    return cell;
}




#pragma mark - 自定义headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, HeaderHeight)];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HeaderHeight)];
    lab.text = [NSString stringWithFormat:@"----我是%@header呀---",self.sectionArray[section]];
    lab.textAlignment = NSTextAlignmentCenter;
    [header addSubview:lab];
    header.backgroundColor = [UIColor blueColor];
    
    return header;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return HeaderHeight;
}
#pragma mark - 自定义footerView
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return FooterHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, FooterHeight)];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FooterHeight)];
    lab.text = [NSString stringWithFormat:@"----我是%@footer呀---",self.sectionArray[section]];
    lab.textAlignment = NSTextAlignmentCenter;
    [footer addSubview:lab];
    footer.backgroundColor = [UIColor brownColor];
    
    return footer;
}





@end


需要注意一点:
当你有多个tableView的时候,你需要判断当前tableView是不是你想要加Header或者Footer的tableView,从而进行添加


补充:
section的view也可以自定义

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

    EHSCheckDetailHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderID];
    if (!header) {
        header = [[EHSCheckDetailHeaderView alloc]initWithReuseIdentifier:SectionHeaderID];
    }
    if (self.dataList.count>0) {
        header.model = self.dataList[section];
    }
    return header;
}

结束语:
最近呢,终于算是把项目的bug改完了,来新公司也半个月了。说实话,这半个月是真的漫长呀~~~~
希望慢慢走上正轨,不管生活,还是工作。
同时,希望自己的学习计划不要搁浅,哈哈哈哈2333333......

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,844评论 25 709
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,065评论 3 119
  • 何塞·阿尔卡蒂奥·布恩迪亚是西班牙人的后裔,住在远离海滨的一个印第安人的村庄。他与乌尔苏拉新婚时,由于害怕像姨母与...
    胡妙妙阅读 1,621评论 0 0
  • 晚上心情很好,19:00已经完成健身(游泳+瑜伽一节),准备回家,正感叹着岁月静好。发现电动车后轮没气,于是赶紧电...
    柚子妹宠自己阅读 4,505评论 1 0

友情链接更多精彩内容