我对快速语言和iOS开发整体都是新手,所以请原谅我缺乏基础知识.以前我尝试并成功实现了多个UITableView自定义部分,方法是创建一个创建TableViewCell的xib文件,然后将其加载到我的主ViewController中,并按以下代码返回:
var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView
return customView
Run Code Online (Sandbox Code Playgroud)
但是因为我开始得到"没有表格单元被重用的索引路径"我回到绘图板并尝试以编程方式创建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
} …Run Code Online (Sandbox Code Playgroud)