Inset Grouped样式UITableView实现分组头部后布局不符合预期
问题描述
- 我在开发中使用了包含两个原型单元格、样式为 Inset Grouped 的
UITableView。
- 未实现
viewForHeaderInSection代理方法时,页面输出效果正常。
- 添加
viewForHeaderInSection代理方法后,页面出现布局错位的问题。
- 最终期望达成的分组头部布局效果如下。

当前实现代码
extension DashboardVC: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 4 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 45 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { return 70 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerCell = tableView.dequeueReusableCell(withIdentifier: "TblHeaderCell") as! TblHeaderCell return headerCell } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TblSectionCell", for: indexPath) as! TblSectionCell return cell } }
核心疑问
如何调整现有代码,才能实现符合预期的分组头部布局效果?
内容的提问来源于stack exchange,提问作者Nikunj Kumbhani
相关产品推荐
相关产品推荐

