Ios 在头视图中正确返回UIView第8节swift
我对swift语言和iOS开发整体来说是相当陌生的,所以请原谅我缺乏基础知识。之前,我尝试并成功实现了多个分区UITableView自定义分区,方法是创建一个xib文件,创建TableViewCell,然后将其加载到我的主ViewController中,并按以下代码返回:Ios 在头视图中正确返回UIView第8节swift,ios,iphone,uitableview,swift,uiview,Ios,Iphone,Uitableview,Swift,Uiview,我对swift语言和iOS开发整体来说是相当陌生的,所以请原谅我缺乏基础知识。之前,我尝试并成功实现了多个分区UITableView自定义分区,方法是创建一个xib文件,创建TableViewCell,然后将其加载到我的主ViewController中,并按以下代码返回: var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView retur
var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView
return customView
但是,自从我开始得到“没有被重用的表单元格的索引路径”后,我回到绘图板,尝试以编程方式创建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
}
...
正如你们所看到的,我试图创建一个布局,我希望我的标签和按钮水平排列,但不知何故,逻辑不起作用,我尝试禁用视图本身的自动调整大小约束,但这也不起作用。请帮忙 您从未将标签或按钮添加到“视图”中。此外,您的大小也没有意义——您将标签和按钮的宽度设置为表视图宽度的1/2,但在约束条件中,您有80个点的空间,因此无法使用。在任何情况下,使用“自动布局”时都不应设置任何帧。去掉这些,并向标签或按钮添加垂直约束,以及一个布局选项以垂直对齐它们。此外,还需要将标签和按钮添加到视图中(并在添加约束之前添加)。这样应该行得通
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
}