簡體   English   中英

如何獲取分組表視圖的標題視圖?

[英]How to get the header view of a grouped table view?

我希望獲得分組表視圖的所有視圖以更改標簽顏色並設置背景顏色。

我找到了答案,不可能得到表視圖部分的標題視圖。 但是您可以實現委托tableView:viewForHeaderInSection:重新創建標題視圖和標簽。 以下代碼將為您提供相同的標題視圖和確切的標簽。

- (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] initWithFrame:CGRectMake(20.0f, 5.5f, 300.0f, 30.0f)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:16.5];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
    view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [view addSubview:label];

    return view;
}

你找到了解決方案,這很棒。

一些建議:

  1. 不要將CGRect硬編碼為幀的寬度,而是使用self.view.size.width作為寬度(例如,如果您處於橫向或者Apple引入了具有不同屏幕尺寸的iPhone);

  2. 您可能希望對標簽和包含標簽的視圖使用autoresizingMask ,以便它們在屏幕方向更改時調整大小,或者確保在方向更改時調用[self.tableview reloadData] ;

  3. 這顯然是一個單行標簽...如果這對你很有用,否則你想使用sizeWithFont:constrainedToSize:lineBreakMode來確定高度,既可以創建標簽/視圖,也可以響應tableView:heightForHeaderInSection:

您還需要添加textColor:

 label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];

暫無
暫無

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

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