UITableView .grouped 类型去除顶部间距

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

UITableView .grouped 类型去除顶部间距_第1张图片

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

tableView.tableFooterView = UIView()

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

UITableView .grouped 类型去除顶部间距_第2张图片

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

UITableView .grouped 类型去除顶部间距_第3张图片

对应代理中 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 类型去除顶部间距_第4张图片

 

你可能感兴趣的:(UITableView .grouped 类型去除顶部间距)