ios – 通过Storyboard和新的Xib实现自定义表部分标题

这是我做的:

我创建了一个自定义xib文件,它具有用于自定义表部分标题的小UIView.
分类自定义xib文件.

我想添加到tableView作为标题.我已经看了几个资源,但他们似乎是过时的或缺少信息.

查看文档,我看到一个引用添加一个自定义标题与以下说明:

To make the table view aware of your header or footer view,you need to register it. You do this using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method of UITableView.

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

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

更多信息:
我有一个storyboard场景,里面有一个tableView. tableView是一个链接自定义类,tableView对象在父视图的ViewController文件中有一个插座.

父ViewController都是UITableViewDataSourceDelegate和UITableViewDelegate.再次,我能够实现没有问题的自定义单元格.除了标题,我甚至不能以任何方式修改标题.

我尝试调用方法[[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;
}

解决方法

您不需要在xib中设置标识符 – 您只需要在注册时使用相同的标识符,并且当您将标题视图排队时.在viewDidLoad方法中,我注册了这样的视图:
[self.tableView registerNib:[UINib nibWithNibName:@"Header1" bundle:nil] forheaderfooterViewReuseIdentifier:@"header1"];

然后,在委托方法中:

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...