将节标题视图添加到表

时间:2016-11-22 09:54:17

标签: ios objective-c

我想知道我做错了什么:

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

}

该方法假设为我的表创建标题视图,但它没有。

4 个答案:

答案 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;

答案 1 :(得分:1)

只需在viewDidLoad()

中添加
self.tableView.estimatedSectionHeaderHeight = 80;

答案 2 :(得分:1)

添加此方法

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

答案 3 :(得分: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;

}