繁体   English   中英

将节标题视图添加到表

[英]Add section header view to table

我想知道我在做什么错:

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

}

该方法假定为我的表创建标题视图,但事实并非如此。

您可以执行两种方式。 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;

只需在viewDidLoad()中添加它

self.tableView.estimatedSectionHeaderHeight = 80;

添加此方法

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

首先设置页眉的高度

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM