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

UITableView .grouped 类型去除顶部间距-LMLPHP

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

tableView.tableFooterView = UIView()

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

UITableView .grouped 类型去除顶部间距-LMLPHP

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

UITableView .grouped 类型去除顶部间距-LMLPHP

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

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

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

UITableView .grouped 类型去除顶部间距-LMLPHP

05-11 20:19