更改节标题标题的背景颜色(普通tableview)

时间:2010-07-27 14:20:28

标签: iphone uitableview iphone-sdk-3.1 uicolor

我的应用程序看起来就像iphone上的默认地址簿应用程序。对于每个字母表,地址簿(A,B ...)中的部分标题为灰色渐变背景颜色。是否可以将该默认颜色覆盖为其他一些色调?

2 个答案:

答案 0 :(得分:4)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 25.0)];

    // create the button object
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor blackColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    headerLabel.frame = CGRectMake(0.0, 0.0, 320.0, 22.0);

    // If you want to align the header text as centered
    //headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);

    headerLabel.text = @"Name of header";

    [customView addSubview:headerLabel];

    return customView;

}

答案 1 :(得分:1)

您可以通过在UITableViewDelegate中实现以下方法来替换UITableView的节头:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

您可能还需要实现height方法以确保正确显示新的剖面视图:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

要更改节标题的颜色,请在其中创建一个UIVmageView的UIView,以及该节文本的UILabel。将图像添加到UIImageView,以了解背景的外观。

AFAIK,你无法操纵剖面视图的tintColor。