通过Storyboard和新的Xib实现自定义表节标题

Ale*_*lex 9 ios

这是我做的:

我创建了一个自定义xib文件,其中有一个用于自定义表节标题的小UIView.我将自定义xib文件归类.

我想将它添加到tableView作为标题.我查看了一些资源,但它们似乎已经过时或缺少信息.

查看文档,我看到了添加自定义标头的参考,其中包含以下说明:

要使表视图知道页眉或页脚视图,您需要注册它.您可以使用registerNib:forCellReuseIdentifier:或registerClass:forCellReuseIdentifier:UITableView的方法.

当我将tableView添加到我的故事板视图时,很容易在XCode中为其分配重用标识符.我甚至能够创建一个自定义单元格xib文件,它在XCode中也有一个重用标识符的位置.

当我为节头创建自定义UIView时,它没有重用标识符的条目.没有这个,我不知道如何使用registerNib:forCellReuseIdentifier.

更多信息:我有一个tableView内部的故事板场景.它tableView是一个链接的自定义类,tableView对象在父视图的ViewController文件中有一个插座.

父母ViewControllerUITableViewDataSourceDelegateUITableViewDelegate.再一次,我能够毫无问题地实现自定义单元格.除了标题之外,我甚至无法以任何方式修改标题.

我尝试[[self tableHeaderView] setBackgroundColor:[UIColor clearColor]];从自定义tableView类调用该方法,没有任何反应.我尝试使用ViewController插座名称在父类中使用此方法,如下所示: [[self.tableOutlet tableHeaderView] setBackgroundColor:[UIColor clearColor]];

任何帮助将不胜感激.

编辑:(无法将背景更改为透明)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    HeaderView *headerView = [self.TableView dequeueReusableHeaderFooterViewWithIdentifier:@"tableHeader"];

    // Set Background color
    [[headerView contentView] setBackgroundColor:[UIColor clearColor]];

    // Set Text
    headerView.headerLabel.text = [self.sectionArray objectAtIndex:section];

    return headerView;
}
Run Code Online (Sandbox Code Playgroud)

rde*_*mar 20

您不需要在xib中设置标识符 - 您只需在注册时使用相同的标识符,并在标题视图出列时使用.在viewDidLoad方法中,我注册了这样的视图:

[self.tableView registerNib:[UINib nibWithNibName:@"Header1" bundle:nil] forHeaderFooterViewReuseIdentifier:@"header1"];
Run Code Online (Sandbox Code Playgroud)

然后,在委托方法中:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header1"];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100;
}
Run Code Online (Sandbox Code Playgroud)