基于NIB的标头的典型过程为:
UITableViewHeaderFooterView
至少创建一个带有标签出口的子类。您可能还想给它一些标识符,通过它可以反向工程此头对应的部分。同样,您可能希望指定一个协议,标题可以通过该协议将事件通知给视图控制器(例如单击按钮)。因此,在Swift 3及更高版本中:
// if you want your header to be able to inform view controller of key events, create protocol
protocol CustomHeaderDelegate: class { func customHeader(_ customHeader: CustomHeader, didTapButtonInSection section: Int) }
// define CustomHeader class with necessary delegate
, @IBOutlet
and @IBAction
:
class CustomHeader: UITableViewHeaderFooterView { static let reuseIdentifier = “CustomHeader”
weak var delegate: CustomHeaderDelegate?
@IBOutlet weak var customLabel: UILabel!
var sectionNumber: Int! // you don't have to do this, but it can be useful to have reference back to the section number so that when you tap on a button, you kNow which section you came from; obvIoUsly this is problematic if you insert/delete sections after the table is loaded; always reload in that case
@IBAction func didTapButton(_ sender: AnyObject) {
delegate?.customHeader(self, didTapButtonInSection: section)
}
}
创建NIB。就个人而言,我为NIB提供与基类相同的名称,以简化项目中文件的管理并避免混淆。无论如何,关键步骤包括:
创建视图NIB,或者如果您以空的NIB开始,则将视图添加到NIB;
将视图的基类设置为您的UITableViewHeaderFooterView
子类(在我的示例中为CustomHeader
);
在IB中添加您的控件和约束;
胡克@IBOutlet
在您的银行代码网点引用;
将按钮连接到@IBAction
; 和
对于NIB中的根视图,请确保将背景色设置为“默认”,否则您将收到有关更改背景色的烦人警告。
在viewDidLoad
视图控制器中,注册NIB。在Swift 3及更高版本中:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: CustomHeader.reuseIdentifier)
}
在中viewForHeaderInSection
,使用您在上一步中指定的相同标识符出队可重用视图。完成此操作后,您现在就可以使用插座了,您无需对以编程方式创建的约束进行任何操作,等等。唯一需要考虑的事情(为了使按钮的协议起作用)就是指定其委托。例如,在Swift 3中:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeader") as! CustomHeader
headerView.customLabel.text = content[section].name // set this however is appropriate for your app's model
headerView.sectionNumber = section
headerView.delegate = self
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 44 // or whatever }
显然,如果要将视图控制器指定delegate
为标题视图中按钮的,则必须遵循该协议:
extension ViewController: CustomHeaderDelegate {
func customHeader(_ customHeader: CustomHeader, didTapButtonInSection section: Int) {
print("did tap button", section)
}
}
当我列出所有涉及的步骤时,这一切听起来都令人困惑,但是一旦完成一两次,它真的很简单。我认为这比以编程方式构建标题视图更简单。
在马特的回答,他抗议:
问题很简单,就是您不能仅仅通过在身份检查器中声明将UIView
笔尖神奇地变成笔尖UITableViewHeaderFooterView
。
这根本是不正确的。如果使用上述基于NIB的方法,则为此头视图的根视图实例化的类是UITableViewHeaderFooterView
子类,而不是UIView
。它实例化为NIB根视图的基类指定的任何类。
但是,正确的是,contentView
在这种基于NIB的方法中未使用此类的某些属性(尤其是)。它确实应该是可选属性,就像textLabel
和detailTextLabel
一样(或者更好的是,它们应该UITableViewHeaderFooterView
在IB中添加适当的支持)。我同意这对Apple来说设计不佳,但它给我留下了草率,特质的细节,但鉴于表视图中的所有问题,这都是一个小问题。例如,非常不寻常的是,这些年来,我们仍然无法在情节提要中完全制作原型页眉/页脚视图,而完全必须依靠这些NIB和类注册技术。
但是,不能断定一个人不能使用register(_:forHeaderFooterViewReuseIdentifier:)
,这是一种自iOS 6起就一直在使用的API方法,这是不正确的。我们不要将婴儿带洗澡水扔掉。