ios - UIViewController 中的 tableHeaderView

标签 ios swift uiviewcontroller

我想在我的 UIViewController 中使用 tableview,并且我想使用“tableHeaderView”,但我相信默认情况下,tableHeaderView 是“nil”

有没有办法初始化它?或者换句话说,如何在 UIViewController 中使用“tableHeaderView”?

谢谢

最佳答案

如果你想为整个表格 View 制作标题,那么你会想做这样的事情

override func viewDidLoad() {
    super.viewDidLoad()
    let header  = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 10))
    header.backgroundColor = UIColor.blackColor()
    tableView.tableHeaderView = header
}

或者如果你想在表格 View 的一部分中添加标题,你可以这样做

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //give height for header
    return 10
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //you want to provide your view here
    return nil
}

关于ios - UIViewController 中的 tableHeaderView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35188369/

相关文章:

ios - 如何获取这段 Swift 代码以接收 PHP 脚本的返回值?

ios - 当我尝试将数据保存到 Firebase 时,它​​只会更新现有数据、Swift、Firebase

ios - 检查 View Controller 是否通过动画成功解除

c# - 更改呈现 View Controller 约束

ios - 密码更改 Parse swift

ios - Xcode 14 弃用了位码 - 但为什么呢?

ios - 动画(旋转)UIBarButtonItem 自定义按钮

ios - Swift 3 iOS -Google GMSAutoComplete 自升级后出现故障

ios - UIAlertAction 中空文本字段的摇动动画

uiviewcontroller - 标题较长时,在iOS 7中将backBarButtonItem重命名