关于iOS tableview自定义区头

很多人自定义区头时继承与UIView,本身没有问题,但在ViewController调用时就要下功夫了。其实区头继承与UITableViewHeaderFooterView才是最实用的。毕竟还要重用滴!

废话不多,代码驾到!

#import <UIKit/UIKit.h>


@interface HeadFooterView : UITableViewHeaderFooterView


@property(strong,nonatomic)UILabel*month;


@property(strong,nonatomic)UILabel*enter;


@property(strong,nonatomic)UILabel*outLab;


@property(strong,nonatomic)UILabel*outMoney;


@property(strong,nonatomic)UILabel*enterMoney;


.m实现

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{

    

    if (self=[super initWithReuseIdentifier:reuseIdentifier]) {

       

        float titleW=50;

        

        float diss=80;

        

        float contextW=(SCREEN_W-3*TAP-3*titleW-diss)/2;

        

        _month=[[UILabel alloc]initWithFrame:CGRectMake(TAP, 0, titleW, 30)];

        //_restultLab.text=@"qqqqq";

        _month.textColor=[UIColor blackColor];

        _month.font=[UIFont systemFontOfSize:15];

        _month.textAlignment=NSTextAlignmentCenter;

        [self.contentView addSubview:_month];

        

        _outLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_month.frame)+diss, 0, titleW, 30)];

        _outLab.text=@"转入:";

        _outLab.textColor=[UIColor blackColor];

        _outLab.font=[UIFont systemFontOfSize:15];

        _outLab.textAlignment=NSTextAlignmentCenter;

        [self.contentView addSubview:_outLab];

        

        _enterMoney=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_outLab.frame), 0, contextW, 30)];

        //_outLab.text=@"转入:";

        _enterMoney.textColor=[UIColor redColor];

        _enterMoney.font=[UIFont systemFontOfSize:15];

        _enterMoney.textAlignment=NSTextAlignmentLeft;

        [self.contentView addSubview:_enterMoney];

        

        _outLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_enterMoney.frame)+TAP, 0, titleW, 30)];

        _outLab.text=@"转出:";

        _outLab.textColor=[UIColor blackColor];

        _outLab.font=[UIFont systemFontOfSize:15];

        _outLab.textAlignment=NSTextAlignmentCenter;

        [self.contentView addSubview:_outLab];

        

        _outMoney=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_outLab.frame), 0, contextW, 30)];

        //_outLab.text=@"转出:";

        _outMoney.textColor=[UIColor greenColor];

        _outMoney.font=[UIFont systemFontOfSize:15];

        _outMoney.textAlignment=NSTextAlignmentLeft;

        [self.contentView addSubview:_outMoney];

            

    }

        


    

    return self;

}



特意提醒一下:如果想作为区头直接调用

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

如果是区尾,- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section


��:

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

    

          

        HeadFooterView*headV=[tableView  dequeueReusableHeaderFooterViewWithIdentifier:@"headv"];

        

        if (!headV) {

            

          headV=[[HeadFooterView alloc]initWithReuseIdentifier:@"headv"];


        }

        

        headV.month.text=@"9";

        headV.enter.text=@"转入";

        headV.outLab.text=@"转出";

        headV.enterMoney.text=@"+9999";

        headV.outMoney.text=@"-999";


        return headV;

   

 }

    

   

    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值