Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法更改UITableView中节标题的文本属性_Ios_Swift_Uitableview - Fatal编程技术网

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

Ios 无法更改UITableView中节标题的文本属性,ios,swift,uitableview,Ios,Swift,Uitableview,我很确定我已经尝试过重写处理UITableViewHeaderFooterView的每个函数,以更改tableView节中标题的文本、字体和文本颜色 我尝试使用从另一个答案中找到的代码: override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header: UITableViewHeaderFooterView =

我很确定我已经尝试过重写处理UITableViewHeaderFooterView的每个函数,以更改tableView节中标题的文本、字体和文本颜色

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

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"
}
这成功地更改了标题的背景色。contentView,但是,只要我添加一行代码来更改textLabel的文本,就不会显示任何内容(包括背景色)


我可以更改标题为headerinsection的文本,但是中的代码将显示headerview不会影响文本属性。覆盖viewForHeaderInSection似乎也没什么作用。有人请帮忙

在尝试在那里设置任何内容之前,请确保您实际查找的属性具有setter

问题 您只设置了
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 }
解决方案 有一个解决方案可以通过使用

自定义类实例在包含时的外观的步骤 在容器类的实例或层次结构中的实例中, 使用appearanceWhenContainedIn:获取 班级。例如,要修改工具栏按钮的外观,请基于 包含导航栏的对象:

我们的案例

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

解决方案2 您还可以创建自己的
UIView
子类,并将其显示在

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

通过这种方式,您可以修改任何您想要的内容,而不是尝试自定义在自定义时非常有限的内容。在尝试在此处设置任何内容之前,请确保您实际查找的属性具有setter

问题 您只设置了
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 }
解决方案 有一个解决方案可以通过使用

自定义类实例在包含时的外观的步骤 在容器类的实例或层次结构中的实例中, 使用appearanceWhenContainedIn:获取 班级。例如,要修改工具栏按钮的外观,请基于 包含导航栏的对象:

我们的案例

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

解决方案2 您还可以创建自己的
UIView
子类,并将其显示在

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

通过这种方式,您可以修改任何您想要的内容,而不是试图自定义一些在自定义方面非常有限的内容。这可能是一个重复的问题,但在阅读(大量)不同的解决方案之后,在试图解决这一问题时,他们中似乎没有人能够澄清谜团中的一个重要部分,我认为,在这个问题上提供一些额外的信息将是建设性的

TL;博士 将标题ForHeaderInSection与viewForHeaderInSection一起使用在viewForHeaderInSection中创建并返回UILabel,并在heightForHeaderInSection中设置标题的高度

更透彻的解释 问题的根源:
由于不正确使用willDisplayHeaderView,我无法更改文本属性。这是因为此函数只应在头的UIView(或UIView的子类)已在别处实例化后使用。除非通过titleForHeaderInSection或viewForHeaderInSection创建标题视图,否则不会创建标题视图。

参考文件中标题ForHeaderin部分:

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

所以,百万美元的问题是:为什么我不能在viewForHeaderInSection中更改文本,或者除非我在titleForHeaderInSection中也为节标题设置文本,否则我不能显示HeaderView?
原来这是一个诡计问题!某种程度上。

在使用viewForHeaderInSection的情况下,文本实际上正在更改-您只是看不到它。当您使用titleForHeaderInstruction设置节标题的文本时,swift会自动为该特定节创建具有默认高度约束的UITableViewHeaderFooterView。创建页眉视图的另一个选项是在viewForHeaderInSection中创建并返回UIView类型的类,但是此处的警告是不会创建默认高度约束,因此,最终将得到高度为零的UIView容器,它将不会显示在屏幕上,但通过覆盖heightForHeaderInSection为其提供一些高度,可以很容易地解决这一问题。

在使用willDisplayHeaderView的情况下,存在的问题是由于错误地实现了该功能。必须通过在titleForHeaderInSection中返回非空字符串值或在viewForHeaderInSection中返回UIView类类型的对象来创建UIView类标题,然后才能使用DisplayHeaderView。这是因为函数中名为“view”的参数必须已经存在,才能更改其属性。看起来有点像一个不需要动脑筋的人

解决方案1 只需确保从TitleForHeaderSection返回一个非空字符串值;这样做将确保头视图对象被实例化。使用ViewForHeaderSection可根据您的喜好设置标题的属性

解决方案2 重写函数viewForHeaderInSection并创建从UIView继承的新对象(也称为UILabe