更改标题区域中标题的颜色

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

{

}

你好
我在objective c方面非常新手.......................通过这种方法,我们可以获取标题部分的标题。但是如何更改该字符串的颜色呢?我能做到吗........如果有人知道,请告诉我。

问候

4个回答

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;
}

2

注意,你返回的视图不会被UITableView保留(至少在OS 3.1.2中似乎存在这个问题)。这会导致难以找到的崩溃,在执行viewDidLoad之前就发生了。

表格视图并不像你想象的那样按需获取你的视图。它会请求所有视图,然后再次请求所有视图,有时还会请求多次,因此每次请求都生成视图非常低效。


2
使用tableView:viewForHeaderInSection:代替。这样,您可以配置一个UILabel或其他内容,包括字体、透明度、颜色等,并且tableview将使用该视图作为标题,而不是默认视图。

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接