如何重新创建与默认tableView:viewForHeaderInSection相同的默认UIView?

7

我尝试实现了

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

要获取黑色区域的标题文本标签,而不是白色区域的默认标签,但我的标签与SDK创建的默认标签非常不同,很丑。

如何重新创建具有与SDK完全相同的UIView?

来自Apple文档:

讨论
表格视图使用固定的字体样式作为节标题。如果您想要不同的字体样式,请在委托方法tableView:viewForHeaderInSection:中返回自定义视图(例如,UILabel对象)。

4个回答

13
尽管这不完全正确,但我希望有人能改进我的方法。文本是正确的(像默认的白色一样,但你可以更改它)。背景渐变和不透明度不太完美(太浅了??)--这里可能需要进行一些微调。
在此处获取sectionheaderbackground.png:http://img5.imageshack.us/img5/6616/sectionheaderbackground.png
// Create a stretchable image that emulates the default gradient
UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];

// Create the view for the header
CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0);
UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
sectionView.alpha = 0.9;
sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal];

// Create the label
CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0);
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
sectionLabel.text = @"Test section label";
sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
sectionLabel.textColor = [UIColor whiteColor];
sectionLabel.shadowColor = [UIColor grayColor];
sectionLabel.shadowOffset = CGSizeMake(0, 1);
sectionLabel.backgroundColor = [UIColor clearColor];
[sectionView addSubview:sectionLabel];
[sectionLabel release];

// Return the header section view
return sectionView;

谢谢你的回答!我会尝试并告诉你结果。这里的诀窍是使用现有图像作为背景颜色:) 我以为他们用了一个带名称的颜色:p - Hoang Pham
正如DavidD在下面回答的那样,return nil看起来最本地化。 - Ivan Marinov
当然,返回nil会给出最本地化的外观,但它不允许您自定义任何内容,比如文本颜色,这正是问题所问的。 - dferg

1

如果您想获取默认的UITableView分区头部,可以在您的tableview类中实现以下方法:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{   
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22; //returning 0 hides section headers
}

这可能特别有用,如果你在你的应用中使用UISearchDisplayController, 并希望搜索结果以简单的UITableViewStylePlain方式显示,而不是像自定义主tableview的UITableViewStyleGrouped那样。然后你可以实现以下类似的内容:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{   
    if ([self.searchDisplayController.searchBar isFirstResponder]) return nil;
    //create and return your custom UIView here
}

这并不能帮助操作者改变文本的颜色。 - user102008
这是最相关的答案!如果你想返回默认外观的部分标题,只需返回 nil。感谢您的提示! - Ivan Marinov

0
你可以尝试使用shadowColorshadowOffset来模仿它。
例如,我用以下代码将其设置为黑色:
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(1, 1);

通过调整颜色和阴影组合,您可以很好地模仿标签。

嗨Rick,背景怎么样?我应该使用什么颜色? - Hoang Pham
你是将它放在分组表视图中吗?如果是的话,你应该将背景颜色设置为[UIColor clearColor]; - rickharrison
不,瑞克,这不是我想要的。根据我在问题中引用的苹果文档讨论,我想重新创建自定义视图,该视图与苹果默认部分标题相同,但文本颜色为黑色,而不是默认的白色。问题是默认背景如此特殊,我不知道那是什么颜色,看起来像渐变色。 - Hoang Pham
所以您在谈论分组或普通表视图中的段标题。这很重要,所以我问一下它是否在分组表视图中。 - rickharrison
不好意思,我错过了这个信息,它只是普通的表格视图,而不是分组表格。 - Hoang Pham

0
如果您想要重新创建默认/标准的heightForHeaderInSection,请使用以下代码:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

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

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 11, tableView.frame.size.width-100, 21)];

    label.text = @"Text";
    label.textColor = [UIColor colorWithRed:76.0/255 green:86.0/255 blue:108.0/255 alpha:1];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];

    [view addSubview:label];

    return view;
}

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