前言:
直接看效果图吧:
首先,本文展示的是较简单的创建方式,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......
