![](/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.