簡體   English   中英

UITableView標頭和文本顏色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM