gpt4 book ai didi

ios - 为什么 tableview section header views 出现在 table header view 之上?

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:26 26 4
gpt4 key购买 nike

我有一个 UITableViewController,我通过以下代码将其 header 设置为“静态”或“ float ”:

//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)
}

出于某种原因,滚动表格 View 现在会导致表格 View 部分标题在滚动时显示在表格 View 上方。有什么办法可以阻止这种行为吗?

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

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
}

最佳答案

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

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

关于ios - 为什么 tableview section header views 出现在 table header view 之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com