0

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:

enter image description here

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.

Community
  • 1
  • 1
Leon Lucardie
  • 9,541
  • 4
  • 50
  • 70
  • Possible duplicate of [How do you change the colour of a section title in a tableview?](https://stackoverflow.com/questions/39614268/how-do-you-change-the-colour-of-a-section-title-in-a-tableview) – Eugene Dudnyk Jun 13 '19 at 15:53

1 Answers1

1

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;
}
nevermore
  • 15,432
  • 1
  • 12
  • 30