Kah*_*hsn 7 uitableview ios swift
我正在尝试在UITableView中自定义标题.我无法在故事板中找到它,所以我开始添加UIView和UILabel.
这是我的代码:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0,0, tableView.frame.size.width, 60))
headerView.backgroundColor = UIColor.cyanColor()
headerView.layer.borderColor = UIColor.whiteColor().CGColor
headerView.layer.borderWidth = 1.0;
let headerLabel = UILabel(frame: CGRectMake(5,2, tableView.frame.size.width - 5, 30))
headerLabel.text = sectionTitle (this is a variable)
headerLabel.textAlignment = NSTextAlignment.Center
headerLabel.font = UIFont(name: "AvenirNext", size: 18)
headerLabel.textAlignment = NSTextAlignment.Center;
headerView.addSubview(headerLabel)
return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来垂直和水平地定位UILabel视图的中心.我该怎么办?每当我改变字体和大小时它会不会改变?
另外,无论如何我会在故事板中做到这一点吗?以编程方式制作标题真的很难.
deh*_*len 28
您可以在故事板中的UITableView上拖动UIView.该视图将自动添加为您的UITableView标头.请参见下图中的视图层次结构.
接下来在标题视图中创建UILabel并设置自动布局约束:
你最终会得到这样的东西:
编辑:
编辑我的答案以回复您的评论.如果您想为每个部分设置一个自定义标题视图,您可以覆盖,func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?就像您已经在做的那样.
然后创建一个@IBOutlet UIView *headerView;您将在上面提到的委托方法中返回的内容.转到故事板并将UIView从对象选项板拖到您的顶部栏UIViewController/UITableViewController(请参阅附图).
接下来将插座连接到创建的插座UIView.您可以在之后的故事板中设计视图,并IBOutlet为您添加一个视图,UILabel以便为您创建的每个部分标题视图更新其文本内容.
| 归档时间: |
|
| 查看次数: |
16865 次 |
| 最近记录: |