实现思路:
用.plain格式的tableView,tableView 的背景色设置为 UIColor.groupTableViewBackground;
然后在代理中任由你自定义各种高度,避免使用 .grouped 格式的表格。
class ELTableControllerGrouped: UIViewController, UITableViewDataSource, UITableViewDelegate {
var table: UITableView!
var sectionCount = 5
var heightForFooter: CGFloat = 18
var heightForHeader: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
table = UITableView(frame: view.bounds, style: .plain)
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "c")
table.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "v")
view.addSubview(table)
table.backgroundColor = UIColor.groupTableViewBackground
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "c")
cell?.textLabel?.text = "dd d"
return cell!
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return section == sectionCount-1 ? 0 : heightForFooter
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let v1 = tableView.dequeueReusableHeaderFooterView(withIdentifier: "v")!
v1.contentView.backgroundColor = UIColor.groupTableViewBackground
return section == sectionCount-1 ? nil : v1
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionCount
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
}