2016-04-09 4 views
3

私は、そのヘッダIは、次のコードを「静的」または「フローティング」製UITableViewControllertableviewセクションヘッダービューがテーブルヘッダービューに表示されるのはなぜですか?

//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()で次のようにそのIセットアップ:

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はテーブルビューのヘッダーの後ろにあるべきではありませんか?どうすればこれを達成できますか? 輸入の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

だから私は最終的にそれを考え出したと私は考えていなかった、やや恥ずかしい:私も下の完全なコードを提供してきました

enter image description here これより早い。とにかく、次のコードを追加してそれを修正しました:

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

私はデキューでヘッダービューを取得しています。ヘッダービューにアクセスするにはどうすればいいですか? –

+0

if headerView = block1TableView.tableHeaderView { block1TableView.bringSubview(toFront:headerView) } –

関連する問題