繁体   English   中英

tableView.headerView(forSection:0)的高度为零

[英]Height for tableView.headerView(forSection: 0) is nil

当用户按照本教程滚动时,我正在尝试更改标题视图的高度。 但是,当我尝试访问self.songsTable.headerView(forSection: 0)?.frame.size.height (以便我可以更新标题视图的高度)时,该值为nil并且调试器告诉我“标题视图具有没有高度”。 我使用heightForHeaderInSectionviewForHeaderInSection编程方式创建了标题视图。

class SongsViewController {
var currentHeaderHeight: CGFloat = 136

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //Throws error
        guard let currentHeaderHeight = self.songsTable.headerView(forSection: 0)?.frame.size.height else {
            print("header view has no height")
            return
        }
    }

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

    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.frame = CGRect(x: 0, y: 0, width: Screen.width, height: maxHeaderHeight)
        return headerView
    }
}

这可能只是您的代码片段中所缺少的,但是您是否将viewController设置为TableViewDataSource?

class SongsViewController: UIViewController, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        songsTable.dataSource = self
    }

    // ... the rest of your code ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM