当它位于顶部时更改UITableView部分标题

时间:2018-05-15 08:34:08

标签: swift uitableview header

我使用此代码在我的表视图中使用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?'类型的值在强制中键入'HeaderViewTableViewCell' 有没有办法将headerView强制转换为我的自定义类型?

1 个答案:

答案 0 :(得分:2)

首先,我建议您使用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
    }
}

注册标题视图(我正在跳过所有其他不相关的代码):

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(HeaderView.self, forHeaderFooterViewReuseIdentifier: "Header")

    // If you have a nib file for your HeaderView then register nib instead
    // Make sure in our nib file you set class name to HeaderView
    // And the file name is also HeaderView.xib
    // tableView.register(UINib.init(nibName: "HeaderView", bundle: nil) , forHeaderFooterViewReuseIdentifier: "Header")

}

实施委托方法:

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

从UIScrollViewDelegate方法updateHeaders()调用scrollViewDidScroll

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    updateHeaders()
} 

在显示视图之前(在出现任何滚动之前)也更新标题,为此使用UITableViewDelegate方法willDisplayHeaderView

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