UIKit下UITableView分区页脚文本与最后一个动态Cell重叠问题求助
解决方案
你可以按以下步骤逐一排查解决页脚覆盖Cell的问题:
- 显式配置Section Footer自动高度
在viewDidLoad方法中添加如下代码,告知TableView自动计算页脚高度:
// 开启section footer自动高度计算 tableView.sectionFooterHeight = UITableView.automaticDimension // 设置预估高度,可根据页脚文本实际长度调整 tableView.estimatedSectionFooterHeight = 50
同时实现对应高度代理方法,覆盖系统默认固定高度逻辑:
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return UITableView.automaticDimension }
确认TableView样式修改生效
UITableView的style属性为只读属性,仅能在初始化时指定:- 代码创建控制器时,必须使用
init(style: .grouped)初始化 - Storyboard/Xib创建时,需要在Interface Builder的属性检查器中直接修改TableView的Style为Grouped,修改后Section Footer会跟随列表滚动,不会悬浮覆盖Cell
- 代码创建控制器时,必须使用
调整列表刷新时机
你当前在viewDidAppear中调用tableView.reloadData(),此时页面已经完成首次渲染,二次刷新容易出现布局计算偏差,建议将刷新逻辑迁移到viewWillAppear中:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tableView.reloadData() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) updateSwitchState() }
- 兜底适配方案
如果以上方案仍未解决问题,可以手动给TableView底部添加内边距,预留出足够的页脚显示空间:
// bottom数值可根据你的页脚实际高度调整 tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 60, right: 0)
内容的提问来源于stack exchange,提问作者jat
相关产品推荐
相关产品推荐

