如何更改UITableView的分区颜色和文本颜色

5

我有一个UITableView,从plist中填充日期,连续的标题也是从plist中填充到UITableView中的。它给我默认的蓝色分节颜色和白色文本颜色,就像这样...

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


NSString *key = nil;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
{
    key = [self.searchResults objectAtIndex:section];
}
else{
    key = [self.mySections objectAtIndex:section];
}

//    NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:@"%@", key];

 }

现在我需要更改默认文本颜色和部分的颜色,为此我正在实现下面显示的代码。但它给我自己的UIView
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

你想让你的章节标题看起来怎么样?在 tableView: viewForHeaderInSection: 方法中的代码应该让你自定义你想要的章节标题的外观。 - codingNinja
我想让UITableView的每个Section的背景颜色为黑色,上面的文本颜色为红色。但是如何实现呢? - Christien
1个回答

10
为了最好地定制表格部分标题的外观,您需要实现两种方法:第一种方法已经拥有,它应该可以工作,尽管结果不是非常可用。
第二种方法是tableView:heightForHeaderInSection:,它告诉UITableView新部分的高度是多少,它可以像这样简单:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.0f; 
}

编辑:根据评论,这是您的代码和定义标题高度的结果:

final header label look

编辑2:如果您想要红色文本与黑色背景,请像这样更改tableView:viewForHeaderInSection:中的代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor blackColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

编辑3:好的,那么我将尝试将你的第一种方法的代码与第二种方法合并。它将是这样的:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor blackColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];

    NSString *key = nil;
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
    {
        key = [self.searchResults objectAtIndex:section];
    }
    else{
        key = [self.mySections objectAtIndex:section];
    }

    tempLabel.text=[NSString stringWithFormat:@"%@", key];
    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

这应该返回一个带有正确标签和外观的表视图段头。

编辑4:关于所有这些操作的说明:如果您使用tableView:viewForHeaderInSection:,则tableView:titleForHeaderInSection:中放置的任何代码都将被忽略。因此,您需要在tableView:viewForHeaderInSection方法中完成整个片段标题的设置,包括正确的文本。


还要考虑这行代码:tempView.backgroundColor=[UIColor clearColor]. 如果您希望视图具有实际的背景颜色,请不要将其设置为clearColor - nikolovski
实际上,您没有理解我的问题。如果我使用viewForHeaderInSectiontempLabel.text=@"Header Text";,这将给我新的章节(我不想要这个),但是我希望titleForHeaderInSection标题以更新的颜色和文本大小出现在章节中。 - Christien
有没有其他方式...可以获取部分数据的“titleForHeaderInSection”以进行自定义 - Christien
嗯,我想我知道你在烦什么。我马上会再更新我的问题。 - nikolovski
你实现了 tableView:heightForHeaderInSection: 方法吗?另外,尝试将 tempView 的高度调整为 tempLabel 的高度,大约为 44 像素。 - nikolovski
显示剩余3条评论

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