像 tableViewCell 一样为 header 和设计制作一个 customCell。
设计是如何完成的:
SMS EMAIL 和 PUSH NOTIFICATION 被封装在一个 stackView 中,并且已经自动布局,带有 TYPE 标签。
这是层次结构。
这就是Type(2/3)的布局设计。自己准确计算。
这是针对stackView的。
两者都是带有 superView 的自动布局(即单元格)
堆栈视图:
和类型标签。
将此单元格作为 tableView 的 headerView 插入。
如何?
在 viewDidLoad 中:
tableView.register(UINib.init(nibName: String(describing: TableHeader.self), bundle: nil), forCellReuseIdentifier: "headerCell")
在您各自的视图控制器中添加这两个功能。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! TableHeader
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 58
}
这是输出:
对于 iOS 8:
StackView 不存在,所以我们需要改用UIView。因为解释自动布局很棘手,所以我只是分享了我的项目,这更容易理解。你可以从这里结帐
不使用 StackView 的输出如下:
2 意见采取。已给出颜色以便更好地理解。