如何更改UITableView节颜色和文本颜色

时间:2012-12-22 08:16:54

标签: ios xcode uitableview

我有UITableView从plist填充日期,偶数部分的标题已经从UITableView中的plist填充。它为我提供了默认的blue部分颜色和White文字颜色。像这样...

-(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];

 }

。 现在我需要更改此默认文本颜色和Section的颜色,为此我实现下面显示的代码。但它给了我自己的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;
}

1 个答案:

答案 0 :(得分: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方法中的正确文本。

相关问题