UITableViewController

Q:UITableViewController的section header的字体是黑色的,且比较大,怎样才能改小?
A:初始化的时候,style设置成.grouped

Q:怎样在表格下方添加按钮
A: 把按钮添加到tablefooterview就行

Q: 如何在保留单元格分割线的情况下,去掉section上下两根与单元格等长的分割线
A: 有以下几种方法

  1. tablestyle从.groupd 改成 .plain
  2. 将cell separator的颜色改成表格的背景色 …
  3. 将cell separatorstyle改成.none,然后根据自己的需要手动添加

Q: 如何将cell的四个角设置成圆角
A: 设置cell.layer.mask就可以,但需要注意设置的时机,cellForRowAt 回调中,cell的size是默认值320*44
错误实例:

1
2
3
4
5
6
7
8
9
10
11
12
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->  UITableViewCell {
let corners:UIRectCorner = [.topLeft, .topRight, .bottomLeft, .bottomRight]
if !corners.isEmpty {
let borderLayer = CAShapeLayer()
borderLayer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: view.frame.size.width-40, height: cell.bounds.size.height+1))
let path = UIBezierPath(roundedRect: borderLayer.frame,
byRoundingCorners: corners,
cornerRadii: CGSize(width: 8, height: 8))
borderLayer.path = path.cgPath
cell.layer.mask = borderLayer
}
}

正确:

1
2
3
4
5
6
7
8
9
10
11
12
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let corners:UIRectCorner = [.topLeft, .topRight, .bottomLeft, .bottomRight]
if !corners.isEmpty {
let borderLayer = CAShapeLayer()
borderLayer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: view.frame.size.width-40, height: cell.bounds.size.height+1))
let path = UIBezierPath(roundedRect: borderLayer.frame,
byRoundingCorners: corners,
cornerRadii: CGSize(width: 8, height: 8))
borderLayer.path = path.cgPath
cell.layer.mask = borderLayer
}
}

Contents
,