表格的Section HeaderTitle 标题样式自定义

本文介绍如何在iOS开发中自定义UITableView的Section HeaderTitle,通过代理方法实现对每个段头的美化,包括添加图片和文字效果。教程中详细阐述了实现步骤,从调用代理方法到返回自定义效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当一个表格分有多个段(拥有多个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;
}
//###############




一个简单的效果就出来啦~
当然,更多的效果,大家自己设计吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值