首页 > 解决方案 > tableView(_:heightForHeaderInSection:) 不工作

问题描述

我正在UITableView我的UIViewController. 我已经能够让我的文本以自定义方式显示UITableViewCell,但我找不到在单元格之间放置间距的方法。

我已经尝试实现tableView(_:heightForHeaderInSection:),但似乎没有调用它,因为我试图放入打印语句以查看它是否运行。

标签: iosswiftuitableview

解决方案


我遇到了heightForHeaderInSection不起作用的问题,但是在我设置 viewForHeaderInSection 之后,高度就可以正常工作。确保您已在部分中设置视图标题。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
    view.backgroundColor = .white
    return view
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
   return 44
}

推荐阅读