简体   繁体   English

添加iOS UITableView HeaderView(不是节标题)

[英]Adding iOS UITableView HeaderView (not section header)

I want to add a table header (not section headers) like in the contacts app for example: 我想在联系人应用程序中添加一个表头(不是节头),例如: 在此输入图像描述

exactly like that - a label beside an image above of the table. 就像那样 - 桌子上方图像旁边的标签。

I want the all view be scrollable so I can't place those outside of the table. 我希望所有视图都可以滚动,所以我不能把它们放在桌子之外。

How can I do that? 我怎样才能做到这一点?

UITableView has a tableHeaderView property. UITableView有一个tableHeaderView属性。 Set that to whatever view you want up there. 将其设置为您想要的任何视图。

Use a new UIView as a container, add a text label and an image view to that new UIView , then set tableHeaderView to the new view. 使用新UIView作为一个容器中添加一个文本标签和图像视图到新UIView ,然后设置tableHeaderView到新视图。

For example, in a UITableViewController : 例如,在UITableViewController

-(void)viewDidLoad
{
     // ...
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
     [headerView addSubview:imageView];
     UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
     [headerView addSubview:labelView];
     self.tableView.tableHeaderView = headerView;
     [imageView release];
     [labelView release];
     [headerView release];
     // ...
} 

You can do it pretty easy in Interface Builder. 您可以在Interface Builder中轻松完成。 Just create a view with a table and drop another view onto the table. 只需使用表创建一个视图,然后将另一个视图放到表中。 This will become the table header view. 这将成为表头视图。 Add your labels and image to that view. 将标签和图像添加到该视图。 See the pic below for the view hierarchy. 有关视图层次结构,请参见下面的图片。 在Interface Builder中查看层次结构

In Swift : Swift中

override func viewDidLoad() {
    super.viewDidLoad()

    // We set the table view header.
    let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
    cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
    self.tableView.tableHeaderView = cellTableViewHeader

    // We set the table view footer, just know that it will also remove extra cells from tableview.
    let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
    cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
    self.tableView.tableFooterView = cellTableViewFooter
}

You can also simply create ONLY a UIView in Interface builder and drag & drop the ImageView and UILabel (to make it look like your desired header) and then use that. 您也可以简单地在“界面”构建器中创建一个UIView,然后拖放ImageView和UILabel(使其看起来像您想要的标题),然后使用它。

Once your UIView looks like the way you want it too, you can programmatically initialize it from the XIB and add to your UITableView. 一旦你的UIView看起来也像你想要的那样,你可以通过编程从XIB初始化它并添加到你的UITableView。 In other words, you dont have to design the ENTIRE table in IB. 换句话说,您不必在IB中设计ENTIRE表。 Just the headerView (this way the header view can be reused in other tables as well) 只是headerView(这样头文件视图也可以在其他表中重用)

For example I have a custom UIView for one of my table headers. 例如,我有一个自定义UIView用于我的一个表头。 The view is managed by a xib file called "CustomHeaderView" and it is loaded into the table header using the following code in my UITableViewController subclass: 该视图由名为“CustomHeaderView”的xib文件管理,并使用我的UITableViewController子类中的以下代码将其加载到表头中:

-(UIView *) customHeaderView {
    if (!customHeaderView) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView" owner:self options:nil];
    }

    return customHeaderView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set the CustomerHeaderView as the tables header view 
    self.tableView.tableHeaderView = self.customHeaderView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
    headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
    headerView.layer.borderColor=[UIColor blackColor].CGColor;
    headerView.layer.borderWidth=1.0f;

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];

    headerLabel.textAlignment = NSTextAlignmentRight;
    headerLabel.text = @"LeadCode ";
    //headerLabel.textColor=[UIColor whiteColor];
    headerLabel.backgroundColor = [UIColor clearColor];


    [headerView addSubview:headerLabel];

    UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];

    headerLabel1.textAlignment = NSTextAlignmentRight;
    headerLabel1.text = @"LeadName";
    headerLabel.textColor=[UIColor whiteColor];
    headerLabel1.backgroundColor = [UIColor clearColor];

    [headerView addSubview:headerLabel1];

    return headerView;

}

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

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