繁体   English   中英

无法更改UITableView中节标题的文本属性

[英]Cannot change text attributes of section header in UITableView

我敢肯定,我已经尝试过覆盖处理UITableViewHeaderFooterView的每个函数,以更改tableView部分中标头的文本,字体和textColor。

我尝试使用从另一个答案中找到的这段代码:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0) 
    header.textLabel!.textColor = UIColor.whiteColor()
    header.alpha = 0.5 
    header.textLabel!.text = "foobar"
}

这成功地改变了header.contentView的背景颜色,但只要我添加的代码行更改为textLabel的文字,没有什么(包括背景色)显示在所有。

我可以使用titleForHeaderInSection更改文本,但是willDisplayHeaderView中的代码不会影响文本属性。 覆盖viewForHeaderInSection似乎也无济于事。 有人请帮忙!

尝试在此处设置任何内容之前,请确保您实际要查找的属性具有设置器。

问题

您仅设置contentView因为它可以与tintColor一起设置,但是其他属性都标有get ,因此您可以获取该值,但无权对其进行设置。

从文档

 public var tintColor: UIColor! public var textLabel: UILabel? { get } public var detailTextLabel: UILabel? { get } // only supported for headers in grouped style public var contentView: UIView { get } public var backgroundView: UIView? public var reuseIdentifier: String? { get } 

有一个可以使用UIAppearance协议实现的解决方案

若要自定义包含在容器类的实例或层次结构中的实例时的类实例的外观,请使用appearanceWhenContainedIn:获取该类的外观代理。 例如,根据包含导航栏的对象来修改栏按钮的外观:

我们的情况

UILabel.appearanceWhenContainedInInstancesOfClasses([UITableViewHeaderFooterView.self]).textColor = UIColor.whiteColor()

更多细节

解决方案2

您还可以创建自己的UIView子类并将其显示在

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

这样,您可以修改所需的任何内容,而不必尝试自定义非常有限的内容

这可能是一个重复的问题,但是在阅读(了大量)不同的解决方案之后,它们似乎都没有阐明尝试解决此问题时的一个重要难题,我认为进一步阐明这一点将具有建设性。这个问题。

TL; DR

无论是在结合使用titleForHeaderInSectionviewForHeaderInSection 创建并返回在viewForHeaderInSection一个UILabel并设置在heightForHeaderInSection头的高度。

更详尽的解释

问题的根源:
由于willDisplayHeaderView的使用不当,我无法更改文本属性。 这是因为仅应其他地方实例化标题的UIView(或UIView的子类) 之后才能使用此函数。 除非通过titleForHeaderInSectionviewForHeaderInSection进行创建,否则不会创建标题视图。

从文档中引用titleForHeaderInSection

返回值
一个字符串,用作节标题的标题。 如果返回nil,则该部分将没有标题。

那么,百万美元的问题:为什么我不能更改viewForHeaderInSectionwillDisplayHeaderView中的文本,除非我也为titleForHeaderInSection中的节标题设置了文本?
原来这是一个技巧问题! 有点。

在使用viewForHeaderInSection的情况下,文本实际上正在更改-您只是看不到它。 当您使用titleForHeaderInSection设置节标题的文本时,swift会自动为该节创建一个具有默认高度限制的UITableViewHeaderFooterView。 创建标题视图的另一种方法是在viewForHeaderInSection中创建并返回UIView类型的类,但是要注意的是, 没有创建默认的高度约束 ,因此最终得到高度为零的UIView容器,并且它不会在屏幕上显示-但这可以通过重写heightForHeaderInSection来给它一些高度来解决。

在使用willDisplayHeaderView的情况下,由于不正确地实现该功能而导致存在该问题。 类类型的UIView的报头必须被创建或者通过返回在titleForHeaderInSection一个非空字符串值或返回willDisplayHeaderView之前在viewForHeaderInSection类类型的UIView的目的应该被使用。 这是因为该函数中名为“ view”的参数必须已经存在 ,才能更改其属性。 似乎有点不费吹灰之力。

解决方案1

只需确保您从titleForHeaderInSection返回一个非空字符串值即可 这样做将确保实例化标题视图对象。 使用viewForHeaderInSection可以根据自己的喜好设置标题的属性。

解决方案2

覆盖函数viewForHeaderInSection并创建一个继承自UIView的新对象(aka,UILabel,UITableViewHeaderFooterView,UIImageView或任何您喜欢的对象)。 返回该对象,然后(非常重要!)在heightForHeaderInSection中设置标题的高度。 无需通过这种方法使用titleForHeaderInSection

我还可以提出其他几种解决方案,但实际上这两种是唯一有意义的解决方案。 当然,如果您对使用Apple的默认方案的标题标题感到满意,请省去所有麻烦,并单独使用titleForHeaderInSection

我遇到了同样的问题,并尝试了Darksinge的答案(解决方案2)。 仍然没有为我工作。

就我而言,添加到solution2时,我还需要在willDisplayHeaderView函数中更改textLabel颜色(或textLabel的其他元素)。 似乎内部的UIlabel对象在调用willDisplayHeaderView之前被覆盖。(假设对象是UIlabel的实物,则被覆盖) 这是一些补充说明。

这是代码(SearchTableSectionHeaderFooterView是自定义UITableViewHeaderFooterView类。)

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return tableView.dequeueReusableHeaderFooterView(withIdentifier: SectionHeaderFooterIdentifier) as! SearchTableSectionHeaderFooterView
}

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // header textLabel color should be modified here
    // ref) https://forums.developer.apple.com/thread/60735
    (view as! SearchTableSectionHeaderFooterView).textLabel?.textColor = UIColor.red
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return cells[section].title
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM