Reputation: 6940
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.
Upvotes: 0
Views: 100
Reputation: 1357
FIRST set the height of the header
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
SECOND
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//AginSpliArray
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2-100, 12, 200, 30)];
[label setFont:[UIFont boldSystemFontOfSize:20]];
label.textColor=White;
label.textAlignment=NSTextAlignmentCenter;
label.backgroundColor=Black;
label.layer.cornerRadius=4;
label.clipsToBounds=YES;
NSString *string=//set whatever you want to set;
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:Clear]; //your background color...
return view;
}
Upvotes: 1
Reputation: 11127
Add this method
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
Upvotes: 1
Reputation: 5259
Just add this in viewDidLoad()
self.tableView.estimatedSectionHeaderHeight = 80;
Upvotes: 1
Reputation: 1055
You can do two ways. 1 way:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _tblPsngrDetails.frame.size.width, 20)];
UILabel *lblHeader = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, _tblPsngrDetails.frame.size.width - 10, 20)];
[lblHeader setFont:POPPINS_NOVA_BOLD(14)];
lblHeader.textColor = [UIColor darkGrayColor];
if (section == 0) {
lblHeader.text = @"Contact Information";
}
else if(section == 1)
{
lblHeader.text = @"Passenger Information";
}
view.backgroundColor = [UIColor clearColor];
lblHeader.backgroundColor = [UIColor clearColor];
[view addSubview:lblHeader];
return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0 || section == 1) {
return 30;
}else{
return 1;
}
}
2nd way: Design a view and add it in table view header in viewDidLoad method.
self.tblPaymentStatus.tableHeaderView = your view name;
Upvotes: 1