5

我有UITableView它从 plist 中填充日期,甚至部分的标题是从 plist 中填充的。UITableView它给了我默认的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];

 }

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

1 回答 1

10

为了最好地自定义表格部分标题的外观,您确实需要实现两种方法:第一种方法是您已经拥有的,它应该可以工作,尽管结果不会很有用。

第二种方法是tableView:heightForHeaderInSection:,它告诉UITableView新部分的高度是多少,它可以像这样简单:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.0f; 
}

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

最终标题标签外观

编辑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方法中的正确文本。

于 2012-12-22T09:34:55.807 回答