iOS自定义区头


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

废话不多,代码驾到!


#import 

@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;
   
 }
    
   
    

你可能感兴趣的:(iOS自定义区头)