![](/img/trans.png)
[英]How to change background color of a header column and how to change text color of the header text inside the column?
[英]UITableView header and text color
我目前有一个带有多个部分的UITableView。
每个部分的标题都在委托中定义,如下所示(为测试目的而调整的代码):
[Export("tableView:viewForHeaderInSection:")]
public UIView GetViewForHeader(UITableView tableView, nint section)
{
var header = tableView.DequeueReusableHeaderFooterView("TestHeaderIdentifier");
if(header == null)
header = new UITableViewHeaderFooterView(new NSString("TestHeaderIdentifier"));
header.TextLabel.Text = "Section " + section;
header.TextLabel.TextColor = UIColor.Red;
header.ContentView.BackgroundColor = UIColor.FromRGB(124, 255, 190);
//.. Other customizations
return header;
}
除了一点,即标签的TextColor,这似乎工作正常。
上面的代码导致以下结果:
可以很好地应用背景颜色和文本本身,但是文本颜色仍设置为默认颜色。 可能是此问题的原因?
我已经尝试过:
RegisterClassForHeaderFooterViewReuse
) 两者均无济于事。
我会给你两个解决方案:
首先,在WillDisplayHeaderView
函数中更改textColor:
public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
if (headerView is UITableViewHeaderFooterView)
{
UITableViewHeaderFooterView v = headerView as UITableViewHeaderFooterView;
v.TextLabel.TextColor = UIColor.Red;
}
}
其次,您可以使用自己的自定义视图来代替UITableViewHeaderFooterView
:
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
//var header = tableView.DequeueReusableHeaderFooterView("TestHeaderIdentifier");
//if (header == null)
// header = new UITableViewHeaderFooterView(new NSString("TestHeaderIdentifier"));
//header.TextLabel.Text = "Section " + section;
//header.TextLabel.TextColor = UIColor.Red;
//header.ContentView.BackgroundColor = UIColor.FromRGB(124, 255, 190);
////.. Other customizations
//return header;
UIView view = new UIView();
view.Frame = new CoreGraphics.CGRect(0,100,200,50);
UILabel label = new UILabel();
label.Frame = view.Bounds;
label.Text = "test";
label.TextColor = UIColor.Red;
view.Add(label);
return view;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.