简体   繁体   中英

UITableView header and text color

I currently have a UITableView with multiple sections.

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.

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 )
  • 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:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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