You need to enable JavaScript to run this app. Inset Grouped样式UITableView实现分组头部后布局不符合预期
优惠活动
大模型
产品
解决方案
定价
更多

Inset Grouped样式UITableView实现分组头部后布局不符合预期

问题描述
  • 我在开发中使用了包含两个原型单元格、样式为 Inset Grouped 的 UITableView。
    初始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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.12.03 14:24:03