【发布时间】:2015-02-11 12:21:57
【问题描述】:
我对 Swift 语言和整个 iOS 开发还很陌生,所以请原谅我缺乏基础知识。以前我尝试并成功地通过创建一个 xib 文件创建 TableViewCell 然后将其加载到我的主 ViewController 并按如下代码返回它来成功实现多个分段的 UITableView 自定义部分:
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
}
...
如您所见,我正在尝试创建一个布局,我希望我的标签和按钮水平布局,但不知何故逻辑无法正常工作,我尝试禁用视图本身的自动调整大小约束,但这也没有奏效。请帮忙!
【问题讨论】:
标签: ios iphone uitableview swift uiview