ios - 更改表格标题部分的字体

标签 ios swift uitableview

谁能帮我解决 viewForHeaderInSection 中缺少的代码?

我必须创建一个 UILabel 吗?

究竟应该如何更改表格标题部分的字体?

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return 55.0;//Choose your custom row height
}

func numberOfSections(in tableView: UITableView) -> Int {
    return self.hoursArray.count
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {

    //Code missing!

}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return self.hoursArray[section]
}

最佳答案

删除此功能,它应该可以使用默认标签。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {


}

对于自定义 header ,您可以返回任何 View ,这里是 UIlabel 的示例。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
    let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 30))
    label.textColor = UIColor.red
    label.text = self.hoursArray[section]
    return label
}

关于ios - 更改表格标题部分的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45025181/

相关文章:

ios - 如何为 NSDate 添加一个月?

swift - 如何在表格 View 中获取超出绑定(bind)文本字段的值?

xcode - 如何从单元格中的按钮操作获取 tableview 中的 indexPath?

swift - 在 Swift 中跳出内部 For 循环的方法

uitableview - 尝试(但失败)更改 UITableView 下的颜色

ios - uitableViewCell 图像到新 View

ios - 第三方开发者可以远程控制HomePod吗?

ios - Objective-C:2个iPad之间的通信

iphone - 自动布局和表格 View 标题

ios - 如何以美国风格格式化恰好是电话号码的 UITextfield 文本?