在 swift 中使用 Nib 中的 AutoLayout 在 UITable 上设置自定义 HeaderView



嗨,我想知道如何为我的UITableView实现自定义标题并使用它的自动布局进行正确定位。

我现在可以显示单元格,但既不应用水平或垂直自动布局。

在我的表视图控制器中,我将标头视图变量设置为我的自定义笔尖,如下所示:

@IBOutlet var view: UIView!
override init(frame: CGRect) { // for using CustomView in code
    super.init(frame: frame)
    self.setup()
}
required init(coder aDecoder: NSCoder) { // for using CustomView in IB
    super.init(coder: aDecoder)
    self.setup()
}
private func setup() {
    NSBundle.mainBundle().loadNibNamed("CustomHeader", owner: self, options: nil)
    self.addSubview(view)
}

在视图类中调用:

override init(frame: CGRect) { // for using CustomView in code
    super.init(frame: frame)
    self.setup()
}
required init(coder aDecoder: NSCoder) { // for using CustomView in IB
    super.init(coder: aDecoder)
    self.setup()
}
private func setup() {
    NSBundle.mainBundle().loadNibNamed("ChallengeHeader", owner: self, options: nil)
    self.addSubview(view)
}

它有一些虚拟内容,我将动态文本设置为内部标签。约束设置正确,我让它在表格单元格上工作正常。

我有这个将视图显示为标题

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if let view = headerView {
        return view
    }
    return nil
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if let head = headerView {
        head.setNeedsLayout()
        head.layoutIfNeeded()
        let height = head.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        let headerFrame = head.frame

        Debug.log("Header Height (height)")
        return 100.0
    }
    return 0
}

目前我返回 100.0,因为高度始终为 0,所以我在这里缺少什么来使自动布局发挥作用?还是我需要以编程方式设置 f/e 宽度?

编辑:忘了添加,xib 的宽度为 400,似乎也保持 400 宽度。

您的实现存在一些问题。

首先,不应将headerView添加为self.view的子视图。

其次,如果您有多个部分,则需要为每个部分使用新的自定义视图,不应重复使用它。

因此,您的代码应如下所示:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return NSBundle.mainBundle().loadNibNamed("CustomHeader", owner: nil, options: nil)[0] as? UIView
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
}

您不需要任何帧操作。只需确保在viewForHeaderInSection中返回正确初始化的CustomHeader,一切正常即可。

如果您需要更多帮助或支持,请告诉我。


这是我用来测试的一个小代码片段 - 与您的情况的唯一区别是您从 xib 加载 headerView:

override func viewDidLoad (){
    headerView = UIView.new() // here you should have your xib loading
    var insideView = UIView.new();
    insideView.setTranslatesAutoresizingMaskIntoConstraints(false)
    headerView.addSubview(insideView);
    // This is simulating the constraints you have in your xib  
    headerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[view(50)]-|", options: nil, metrics: nil, views: ["view": insideView]))     
    headerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[view]-|", options: nil, metrics: nil, views: ["view": insideView]))
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return headerView
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
}

最新更新