簡體   English   中英

iOS - 表格視圖 - 靜態單元格(已分組) - 更改節標題文本顏色

[英]iOS - table view - static cells (grouped) - change section header text color

概觀

我有一個帶有表視圖的iOS項目,其中包含以下規范:

  • 靜態單元格 (內容未動態填充)
  • 風格分組

  1. 如何更改靜態表視圖的節頭的文本顏色?

您需要創建自己的標題視圖:

在tableview數據源/委托中實現

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(20, 6, 300, 30);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithHue:(136.0/360.0)  // Slightly bluish green
                                 saturation:1.0
                                 brightness:0.60
                                      alpha:1.0];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;

    // Create header view and add label as a subview

    // you could also just return the label (instead of making a new view and adding the label as subview. With the view you have more flexibility to make a background color or different paddings
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)];
    [view autorelease];
    [view addSubview:label];

    return view;
}

也可以做到這一點:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    [[((UITableViewHeaderFooterView*) view) textLabel] setTextColor:[UIColor whiteColor]];
}

.....

只有在為標題添加標題高度后才能查看標題

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 110;
}

在Swift 4.2中

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.textLabel?.textColor = UIColor.OMGColors.dimText
    }
}

無需制作自己的標題視圖 - 這會破壞靜態單元格的目的。 我很驚訝你不能直接在IB的某處設置這個......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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