简体   繁体   English

UITableView标头和文本颜色

[英]UITableView header and text color

I currently have a UITableView with multiple sections. 我目前有一个带有多个部分的UITableView。

The header for each section is defined in the delegate like this (code adjusted for testing purposes): 每个部分的标题都在委托中定义,如下所示(为测试目的而调整的代码):

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

This seems to work fine except for one bit, the TextColor of the label. 除了一点,即标签的TextColor,这似乎工作正常。

The above code results in the following: 上面的代码导致以下结果:

在此处输入图片说明

The background color and text itself are applied fine, but the text color remains set to the default colour. 可以很好地应用背景颜色和文本本身,但是文本颜色仍设置为默认颜色。 What could be the cause of this issue? 可能是此问题的原因?

I've already tried: 我已经尝试过:

  • Registering the header class on the table view instead of constructing it in the delegate method (using RegisterClassForHeaderFooterViewReuse ) 在表视图上注册标头类,而不是在委托方法中构造标头类(使用RegisterClassForHeaderFooterViewReuse
  • Not reusing/dequeing the headers at all, constructing a new instance each time 完全不重用/出列报头,每次都构造一个新实例

Both to no avail. 两者均无济于事。

I would give your two solutions: 我会给你两个解决方案:

First, change the textColor in WillDisplayHeaderView function: 首先,在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;
    }

}

Second, you can use your own custom views instead of UITableViewHeaderFooterView : 其次,您可以使用自己的自定义视图来代替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.

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