本文介绍了InterfaceBuilder 中的 UITableViewHeaderFooterView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 IB 在单独的 .xib 中创建自定义标题部分视图

I want to create a custom header section view in a separate .xib using IB

但是在 IB 中我找不到组件 UITableViewHeaderFooterView(类似于 UITableViewCell)并为其分配重用

However in IB i cannot find a component UITableViewHeaderFooterView (similar to UITableViewCell) and assign a reuse for it

那么如何在自定义标题部分创建?

So how to create in custom header section?

我创建了一个继承自 UITableViewHeaderFooterView 的类 MySection创建一个 .xib,MySection.xib注册xib以供单元重用

I create a class MySection which inherits from UITableViewHeaderFooterViewCreate a .xib, MySection.xibRegister xib for cell reuse

问题是,如何使用initWitReuseIdentifier....

the problem is, how to use the initWitReuseIdentifier....

推荐答案

完全有可能,Apple 提供了一个例子 此处.

It's absolutely possible and Apple provides an example here.

下载示例代码并查看 SectionHeaderView.xib.

Download the sample code and look at the SectionHeaderView.xib.

这样做的方法是创建一个带有单个 UIView 的 xib.然后,将类类型设置为继承自 UITableViewHeaderFooterView 的类.

The way to do it is to create a xib with a single UIView on it. Then, set the class type to a class that inherits from UITableViewHeaderFooterView.

一旦你有一个继承自 UITableViewHeaderFooterView 的类的笔尖,调用以下代码来注册该类以作为页眉或页脚重用:

Once you have a nib with a class that inherits from UITableViewHeaderFooterView, call the following to register the class for reuse as a header or footer:

static NSString * const SectionHeaderViewIdentifier = @"SectionHeaderViewIdentifier";

[self.tableView registerNib:[UINib nibWithNibName:@"SectionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];

要使用视图,实现表委托方法tableView:viewForHeaderInSection:像这样:

To put the view into use, implement the table delegate method tableView:viewForHeaderInSection: like so:

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

    SectionHeaderViewClass *sectionHeaderView = (SectionHeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

    // Do stuff...

    return sectionHeaderView;
}

这篇关于InterfaceBuilder 中的 UITableViewHeaderFooterView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:38