Swift 更改UITableView节标题,当其为';它在上面

Swift 更改UITableView节标题,当其为';它在上面,swift,uitableview,header,Swift,Uitableview,Header,我正在使用以下代码为我的表视图使用来自Xib文件的自定义头: func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let result: Result = (self.responseData?.result![section])! let headerCell = Bundle.main.loadNibNamed("HeaderViewTa

我正在使用以下代码为我的表视图使用来自Xib文件的自定义头:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let result: Result = (self.responseData?.result![section])!

    let headerCell = Bundle.main.loadNibNamed("HeaderViewTableViewCell", owner: self, options: nil)?.first as!  HeaderViewTableViewCell
   headerCell.sectionName.text = "Title"
    if (section == 0) {
        headerCell.sectionName.textColor = UIColor(red: 49/255.0, green: 149/255.0, blue: 213/255.0, alpha: 1)
    }

    return headerCell
}
然后我想在滚动到顶部时更改标题sectionName,我已经尝试了以下代码

let topSection = self.mainTable .indexPathsForVisibleRows?.first?.section
let currentHeader : HeaderViewTableViewCell = self.mainTable .headerView(forSection: topSection!) as HeaderViewTableViewCell
currentHeader.sectionName.textColor = UIColor.red
但我得到了这个错误:无法在强制中将类型为“UITableViewHeaderFooterView”的值转换为类型为“HeaderViewCell”
有没有办法将headerView强制转换为我的自定义类型?

首先,我建议您使用
UITableViewHeaderFooterView
作为标题视图。您可以创建一个子类并添加自定义代码。对于本例,我将使用一个空的子类:

class HeaderView: UITableViewHeaderFooterView {
    override func prepareForReuse() {
        super.prepareForReuse()

        // set you default color (other properties) here.
        // when scrolling fast the view gets reused and sometimes
        // the view that's on top will suddenly appear on the bottom still with the previous values
        textLabel?.textColor = .black
    }
}
注册标题视图(我将跳过所有其他无关代码):

实现委托方法:

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

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header")
    view?.textLabel?.text = "Hello"
    return view
}
创建更新标题的方法:

// Iterates through section header views and 
// checks for positions in relation to the tableview offset
func updateHeaders() {
    var sectionHeaders: [Int: HeaderView?] = [:]

    var i = 0
    while i < numberOfSections {
        sectionHeaders[i] = tableView.headerView(forSection: i) as? HeaderView
        i += 1
    }

    let availableHeaders = sectionHeaders.flatMap { $0.value != nil ? $0 : nil }
    for (index, header) in availableHeaders {
        let rect = tableView.rectForHeader(inSection: index)

        if rect.origin.y <=  tableView.contentOffset.y + tableView.contentInset.top || index == 0 {
            header!.textLabel?.textColor = .red
        } else {
            header!.textLabel?.textColor = .black
        }
    }
}
此外,在显示视图之前(在出现任何滚动之前)也要更新标题,为此,请使用UITableViewDelegate方法
willDisplayHeaderView

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    updateHeaders()
}

首先,我建议您使用
UITableViewHeaderFooterView
作为标题视图。您可以创建一个子类并添加自定义代码。对于本例,我将使用一个空的子类:

class HeaderView: UITableViewHeaderFooterView {
    override func prepareForReuse() {
        super.prepareForReuse()

        // set you default color (other properties) here.
        // when scrolling fast the view gets reused and sometimes
        // the view that's on top will suddenly appear on the bottom still with the previous values
        textLabel?.textColor = .black
    }
}
注册标题视图(我将跳过所有其他无关代码):

实现委托方法:

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

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header")
    view?.textLabel?.text = "Hello"
    return view
}
创建更新标题的方法:

// Iterates through section header views and 
// checks for positions in relation to the tableview offset
func updateHeaders() {
    var sectionHeaders: [Int: HeaderView?] = [:]

    var i = 0
    while i < numberOfSections {
        sectionHeaders[i] = tableView.headerView(forSection: i) as? HeaderView
        i += 1
    }

    let availableHeaders = sectionHeaders.flatMap { $0.value != nil ? $0 : nil }
    for (index, header) in availableHeaders {
        let rect = tableView.rectForHeader(inSection: index)

        if rect.origin.y <=  tableView.contentOffset.y + tableView.contentInset.top || index == 0 {
            header!.textLabel?.textColor = .red
        } else {
            header!.textLabel?.textColor = .black
        }
    }
}
此外,在显示视图之前(在出现任何滚动之前)也要更新标题,为此,请使用UITableViewDelegate方法
willDisplayHeaderView

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    updateHeaders()
}


显示HeadServiceTableViewCell的code@vikingosegundo这是一个UITableViewCell类,其中包括outlets和awakeFromNibwhy要使用UITableViewCell作为标题?@vikingosegundo我想使用自定义视图您的节标题0不是始终位于顶部吗?显示HeadServiceWebTableViewCell的code@vikingosegundo这是一个UITableViewCell类包括outlets和awakefromnib为什么要使用UITableViewCell作为标题?@vikingosegundo我想使用自定义视图您的分区标题0不是始终位于顶部吗?谢谢,但UITableViewHeaderFooterView类不包括XIB文件,我希望它设计我的自定义标题视图我有一个自定义标题,其中包括一个图标和2labelThanks但在更新头中,我希望头强制转换为HeaderView类型,但它崩溃了:let header:HeaderView=self.mainTable.HeaderView(forSection:I)as!HeadService在创建任何头之前调用
scrollViewDidScroll
可能会发生这种情况,在这种情况下,它将崩溃。我编辑了答案来检查标题。谢谢它的工作,我仍然有一些问题,其中有时有两个是红色的\谢谢,但是UITableViewHeaderFooterView类不包括XIB文件,我想让它设计我的自定义标题视图我有一个自定义标题,其中包括一个图标和两个LabelHanks,但在更新标题中,我想将标题转换为HeaderView类型,但它崩溃了:let header:HeaderView=self.mainTable.HeaderView(forSection:i)as!HeadService在创建任何头之前调用
scrollViewDidScroll
可能会发生这种情况,在这种情况下,它将崩溃。我编辑了答案来检查标题。感谢它的工作,我仍然有问题,有时其中两个是红色的\