2016-11-22 78 views
0

我不知道我做错了什么:添加节头以表

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

我的看法类很简单:

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

} 

想创建头鉴于我的表的方法,但它没有。

+0

检查此链接:http://stackoverflow.com/questions/15611374/customize-uitableview-header-section – VJVJ

回答

1

添加此添加此方法

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

它自动计算 –

+0

对不起,添加了错误的方法,使用这一个 – Rajat

+0

好的我试试谢谢:) –

1

你可以做两种方法。 1方式:

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

第二方式: 设计一个视图中和viewDidLoad方法表视图头添加它。

self.tblPaymentStatus.tableHeaderView = your view name; 
+0

这是不是为你工作? – Sanjukta

1

就在viewDidLoad中()

self.tableView.estimatedSectionHeaderHeight = 80; 
1

首先设置标题的高度

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

第二

- (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; 

}