当一个表格分有多个段(拥有多个section)并且每段内容不同时,我们就会在每段的开头进行文字说明。就是我们的SectionHeader Tltle。
对于Header进行自定义时,我们往往会添加图片或者文字进行美化,接下来要说的就是这一过程。
OK,还是先看看实现步骤:
调用代理方法==》自定义美化效果或者文字效果==》自定义好后,返回这个效果。
OK,下面上过程:
首先是调用代理方法,表格有一个关于SectionHeader的函数:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
};
调出来,然后在函数里进行效果自定义:
//###############
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//创建一个用于返回效果的UIView,用来承接文字或图片
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)] ;
customView.backgroundColor=[UIColor orangeColor];
//自定义文字效果
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor orangeColor];
//字体不透明
headerLabel.opaque =NO;
headerLabel.textColor = [UIColor purpleColor];
//
headerLabel.highlightedTextColor = [UIColor blackColor];
//字体效果
headerLabel.font = [UIFont boldSystemFontOfSize:18];
//设置label格式
headerLabel.frame = CGRectMake(10.0, 0.0, 320.0, 30.0);
if (section == 0) {
headerLabel.text = @"测试1";
}else if (section == 1){
headerLabel.text = @"测试2";
}else if (section == 2){
headerLabel.text = @"测试3";
}else if (section == 3){
headerLabel.text = @"测试4";
}
//将自定义的内容添加到UIView上
[customView addSubview:headerLabel];
//返回自定义好的效果
return customView;
}
//别忘了设置高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
//###############
一个简单的效果就出来啦~
当然,更多的效果,大家自己设计吧~