2016-04-09 2 views
3

У меня есть 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 
} 

и что настройки I следующим образом в 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 
} 

}

ответ

4

Так что я, наконец, понял это, и я немного стыдно, что я не думал это раньше. В любом случае, добавив следующий код, исправил его:

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

Я получаю заголовок заголовка dequee. Итак, как ваш ответ, как я могу получить доступ к представлению заголовка –

+0

, если пусть headerView = block1TableView.tableHeaderView { block1TableView.bringSubview (toFront: headerView) } –