Evgeniy Kleban Evgeniy Kleban - 14 days ago 5
iOS Question

Add section header view to table

I wonder what i'm doing wrong:

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

HeaderView *tableHead = [[HeaderView alloc]initWithFrame:CGRectMake(0, 0, self.myTableView.frame.size.width, 40)];
[tableHead initialize];
return tableHead;
}


My view class is simple:

@interface HeaderView ()

@property (nonatomic) UILabel *headerLbl;

@end

@implementation HeaderView


-(instancetype)init{

self = [super init];
return self;
}

-(void)initialize{

[self createUserInterface];
[self createConstraints];
}

-(void)createUserInterface{

self.backgroundColor = [UIColor defaultGray];
_headerLbl = [UILabel new];
_headerLbl.font = [UIFont systemFontOfSize:13];
_headerLbl.textColor = [UIColor fontGray];
_headerLbl.numberOfLines = 0;
[self addSubview:_headerLbl];
}



-(void)createConstraints{

[_headerLbl mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerX.equalTo(self.mas_centerX);
make.centerY.equalTo(self.mas_centerY);
}];

}


That method suppose to create header view for my table, but it doesn't.

Answer

Add this method

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}