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

Mat*_*ers 1 uitableview ios autolayout swift

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

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

在我的tableViewController中,我将headerView变量设置为我的自定义nib,如下所示:

@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)
}
Run Code Online (Sandbox Code Playgroud)

在视图类中调用:

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)
}
Run Code Online (Sandbox Code Playgroud)

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

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

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
}
Run Code Online (Sandbox Code Playgroud)

目前我正在返回100.0,因为高度始终为0,所以我在这里缺少什么使自动布局做它的工作?或者我是否需要以编程方式设置f/e宽度?

编辑:忘记添加,xib的宽度为400,似乎仍保持400宽度.

Cat*_* T. 6

您的实施存在一些问题.

首先,您不应该添加headerViewas子视图self.view.

其次,如果您有多个部分,则每个部分都需要一个新的CustomView,您不应该重复使用它.

所以你的代码应该是这样的:

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
}
Run Code Online (Sandbox Code Playgroud)

您不需要任何帧操作.只要确保你返回正确初始化CustomHeaderviewForHeaderInSection,一切都应该正常工作.

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


这是我用来测试的一个小代码片段 - 唯一不同的是你从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
}
Run Code Online (Sandbox Code Playgroud)