在设置 UITableView 的 style 为 .grouped 类型的时候,发现第一个 cell 的顶部存在大段的间距,而改为 .plain 类型则没有这个间距,效果如下:

UITableView .grouped 类型去除顶部间距

设置了 contentInset 和 heightForHeader 为 0.01 都无效,最后发现是 

tableView.tableFooterView = UIView()

的书写位置有问题,只要调整代码的顺序就可以了,如下:

UITableView .grouped 类型去除顶部间距

调整后再看效果就正常了:

UITableView .grouped 类型去除顶部间距

对应代理中 heightForHeader 和 heightForFooter 的设置如下:

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 10
}
    
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 10
}

 

【参考】UITableViewStyleGrouped模式下烦人的多余间距

 

UITableView .grouped 类型去除顶部间距

 

相关文章:

  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-07-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-02
  • 2021-09-17
  • 2022-01-27
  • 2021-08-11
相关资源
相似解决方案