为什么tableview部分标题视图出现在表标题视图上?

时间:2016-04-09 16:13:47

标签: ios swift uitableview

我有一个UITableViewController标题我通过以下代码“静态”或“浮动”:

//MARK: Scroll view functions
override func scrollViewDidScroll(scrollView: UIScrollView) {
    //Makes sure the header does not scroll with the table view
    var headerRect = CGRect(x: 0, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
    headerRect.origin.y = tableView.contentOffset.y
    headerView.frame = headerRect
}

我在viewDidLoad()中按以下方式设置:

override func viewDidLoad() {     
        kTableHeaderHeight = self.view.frame.height/3
        headerView = tableView.tableHeaderView
        tableView.tableHeaderView = nil
        self.view.addSubview(headerView)
        tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
        tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)
}

由于某种原因,滚动表视图现在会导致表格视图部分标题在滚动时显示在表视图上。有没有办法阻止这种行为?

下面描绘的是浅灰色的表格视图标题和深灰色的标题标题。第0部分不应该在表格视图标题的“后面”吗?我怎样才能做到这一点?

enter image description here 我也提供了以下完整代码: 导入UIKit

class TableViewController: UITableViewController {

private var kTableHeaderHeight: CGFloat = CGFloat()
var headerView: UIView!

// Setting up the table header view
override func viewDidLoad() {
    super.viewDidLoad()
    initialSetup()
}

//MARK: Table view functions
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("eCell")! as UITableViewCell
    return cell
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 5
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // Adding table view header separator
    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor(red: 38/255, green: 38/255, blue: 38/255, alpha:1) //38
    header.textLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1) //headline

    // Adding cell separator
    let separator = UIView(frame: CGRectMake(15, 0, self.view.frame.width,1))
    separator.backgroundColor = UIColor(red: 57/255, green: 57/255, blue: 57/255, alpha: 1)
    header.addSubview(separator)

    header.textLabel!.textColor = UIColor(red: 131/255, green: 131/255, blue: 131/255, alpha: 1)
    self.tableView.sendSubviewToBack(view)
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Section " + String(section)
}

//MARK: Scroll view functions
override func scrollViewDidScroll(scrollView: UIScrollView) {
    updateHeaderView()
}

//MARK: Private Functions
func initialSetup() {
    kTableHeaderHeight = self.view.frame.height/3
    headerView = tableView.tableHeaderView
    tableView.tableHeaderView = nil
    self.view.addSubview(headerView)
    tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
    tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)

}

func updateHeaderView() {
    //Makes sure the header does not scroll with the table view
    var headerRect = CGRect(x: 0, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
    headerRect.origin.y = tableView.contentOffset.y
    headerView.frame = headerRect
}

}

1 个答案:

答案 0 :(得分:6)

所以我终于弄明白了,我有点惭愧,我没有想到这个。无论如何,添加以下代码修复它:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.tableView.bringSubviewToFront(headerView)
}