UITableView .grouped 类型去除顶部间距

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

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

tableView.tableFooterView = UIView()

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

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

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

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

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

原文地址:https://www.cnblogs.com/wx1993/p/10563954.html