更改标题的颜色对于标题部分

时间:2009-10-05 05:20:56

标签: ios iphone objective-c

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

{

}

您好
我是一个非常新的目标c .......................通过这种方法,我们可以获得标题部分的标题。但是如何改变它的颜色字符串?我可以这样做........如果有人知道,请告诉我。

问候

4 个答案:

答案 0 :(得分:5)

您可以尝试以下方式: 包含两个自定义标签的标头

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

    NSString *dateStr = [[self.data allKeys] objectAtIndex:section];
    CGFloat labelWidth = tableView.bounds.size.width / 2;
    CGFloat padding = 5.0;

    UILabel *labelOne = [[UILabel alloc] initWithFrame:CGRectMake
                        (padding, 0, (labelWidth - padding), 22)];
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textAlignment = UITextAlignmentLeft;
    labelOne.text = dateStr;

    UILabel *labelTwo = [[UILabel alloc] initWithFrame:CGRectMake
    (labelWidth, 0, (labelWidth - padding), 22)];
    labelTwo.backgroundColor = [UIColor clearColor];
    labelTwo.textAlignment = UITextAlignmentRight;
    labelTwo.text = @"This is Label TWO";

    [headerView addSubview:labelOne];
    [headerView addSubview:labelTwo];

    [labelOne release];
    [labelTwo release];

    return headerView;
}

答案 1 :(得分:2)

请改用tableView:viewForHeaderInSection:。这样你可以配置一个UILabel或类似的东西(包括字体,透明度,颜色等),tableview将使用该视图作为标题而不是默认视图。

答案 2 :(得分:2)

请注意,UITableView不会保留您返回的视图(OS 3.1.2至少似乎显示此问题)。这导致在执行到viewDidLoad之前发生的难以发现的崩溃。

根据您的想法,表格视图不会按需获取您的观看次数。它会请求所有人,然后再次请求所有人,有时几次,所以在每次请求时生成它们都是非常低效的。

答案 3 :(得分:1)

请使用以下代码&更改UITableView标题的颜色

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,44)];
    tempHeaderView.backgroundColor=[UIColor clearColor];
    UILabel *tempHeaderLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,320,44)];
    tempHeaderLabel.backgroundColor=[UIColor clearColor];
    tempHeaderLabel.text=@"HEADER";
    [tempHeaderView addSubView: tempHeaderLabel];
    return tempHeaderView;
}