簡體   English   中英

設置UITableView Section Header的顏色

[英]Set color of UITableView Section Header

如何將我所有部分的節頭設置為redColor,而不將它們設置為具有相同的標題字符串,並設置字體和字體顏色。

我試過這個,但它擺脫了我的節標題

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    if (section == 1)
        [headerView setBackgroundColor:[UIColor redColor]];
    else
        [headerView setBackgroundColor:[UIColor clearColor]];
    return headerView;
}

我真的很感激一些示例代碼。

提前致謝

如果您只是想要更改標題視圖的背景和字體顏色,可以使用更簡單的方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

有一個非常好的例子/解釋在這里

嘗試自定義節標題視圖。 您可以擴展此方法。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];

    //Put your common code here
    // lets say you need all background color to be white. do this here
     [headerView setBackgroundColor:[UIColor redColor]];

    //Check the section and do section specific contents
    if (section == 1)
        [headerView setText:@"Section 1"];
    else
        [headerView setBackgroundColor:[UIColor clearColor]];

  return headerView;

}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.textLabel.textColor = [UIColor redColor];
    CGRect headerFrame = header.frame;
    header.textLabel.frame = headerFrame;
    [header.contentView setBackgroundColor:[UIColor blueColor]];
}

暫無
暫無

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

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