在設置 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模式下煩人的多余間距