在viewForHeaderInSection iOS 8 swift中正确返回UIView

我对整体语言和iOS的发展相当陌生,所以请原谅我缺乏基础知识。 以前我试过并成功地实现了多个分段的UITableView自定义节通过创build一个xib文件创buildTableViewCell,然后将其加载到我的主ViewController并返回它编码如下:

var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView return customView 

但是,因为我开始得到“没有索引path的表格单元格被重用”我回到绘图板,并尝试以编程方式创build一个UIView并返回它,到目前为止,我已经不成功,但是这是我编码:

 func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!{ if(section == 0) { var view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50)) var label = UILabel(frame: CGRectMake(0,0, tableView.frame.size.width/2, 20)) label.text="My Details" let button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.frame = CGRectMake(0, 0, tableView.frame.size.width/2, 20) button.addTarget(self, action: "visibleRow", forControlEvents:.TouchUpInside) label.setTranslatesAutoresizingMaskIntoConstraints(false) button.setTranslatesAutoresizingMaskIntoConstraints(false) let views = ["label": label,"button":button,"view": view] var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label(20)]-60-[button(20)]-10-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) view.addConstraints(horizontallayoutContraints) return view } ... 

正如你可以看到我想创build一个布局,我希望我的标签和button水平布局,但不知何故逻辑不工作我试图禁用视图本身自动调整约束,但也没有工作太。 请帮忙!

您从未将您的标签或button添加到“查看”。 另外你的尺寸没有意义 – 你将标签和button的宽度设置为表视图的宽度的1/2,但是在你的约束条件下,你有80点的空间值,所以不能工作。 无论如何,当您使用自动布局时,您不应该设置任何框架。 摆脱这些,并为标签或button添加一个垂直约束,以及一个垂直alignment的布局选项。 此外,您需要将您的标签和button添加到您的视图(并在添加约束之前执行此操作)。 像这样的东西应该工作,

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { if(section == 0) { var view = UIView() // The width will be the same as the cell, and the height should be set in tableView:heightForRowAtIndexPath: var label = UILabel() label.text="My Details" let button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.addTarget(self, action: "visibleRow:", forControlEvents:.TouchUpInside) label.setTranslatesAutoresizingMaskIntoConstraints(false) button.setTranslatesAutoresizingMaskIntoConstraints(false) button.setTitle("Test Title", forState: .Normal) let views = ["label": label,"button":button,"view": view] view.addSubview(label) view.addSubview(button) var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|", options: .AlignAllCenterY, metrics: nil, views: views) view.addConstraints(horizontallayoutContraints) var verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0) view.addConstraint(verticalLayoutContraint) return view } return nil } func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50 } 
 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 20 } override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let view = UIView() view.backgroundColor = UIColor.clearColor() return view }