iOS表视图分割线的总结

来源:互联网 发布:通过 相信 马克思 知乎 编辑:程序博客网 时间:2024/05/17 20:29

1.在自定义单元格的底部或者顶部画一条分割线,这种方法简单粗暴,而且可以在任意位置绘制,相对也比较灵活

 let seperateLine = UILabel() seperateLine.backgroundColor=UIColor.groupTableViewBackground

2.利用SectionHead和SectionFoot的高度来实现

如果用这种方法来实现分割线的效果,一定要记住把UITableView的风格设置为grouped,因为plain类型UITableView的section会有一个悬停的效果,用户体验不好。但是,grouped类型默认是有节头和节脚高度的,所以需要自己设置高度,而且不能设置为0,因为设置为0的时候,也是默认高度,应该设置为0.001。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {      if section == 0 {          return 0.001      }      return 15.0 }    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {      return 0.001} func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {        return ""}func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {        let header = view as! UITableViewHeaderFooterView        header.contentView.backgroundColor=UIColor.groupTableViewBackground}

ps:必须得给title赋值,否则view将为nil。通过这种方式也可以为系统的sctionHead或者sctionFoot设置背景颜色以及设置title的字体、颜色等。如果只设置section高度,不给title赋值,此时sctionHead或者sctionFoot为nil即不显示,所以我们看到的颜色将是tableview的背景颜色,通过该方式也可以形成分隔带。

3.自定义sctionHead或者sctionFoot

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {      let view=UIView(frame: CGRect(x: 0, y: 0, width: Screen.Width, height: 1))      view.backgroundColor=UIColor.groupTableViewBackground      return view}func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {      return 1}







原创粉丝点击