UITableViewStyleGroup默认标题视图?

UITableViewStyleGroup默认标题视图?,uitableview,Uitableview,我正在尝试实现一个用于删除整个部分的控件,如果删除按钮位于标题中,则在我的应用程序中效果最好,而不是像UIPopoverView这样的覆盖 在写这个问题的过程中,我找到了答案。只要有一个起点,就很容易了。我得到了大部分代码,其中只有两篇文章,都是从2010年开始的。 然后我又回到了字体颜色,因为分开比较麻烦 三个小问题,都与标签有关 - Font is too narrow - Text color is too dark - Label origin is wrong 默认字体是已知的,所

我正在尝试实现一个用于删除整个部分的控件,如果删除按钮位于标题中,则在我的应用程序中效果最好,而不是像UIPopoverView这样的覆盖



在写这个问题的过程中,我找到了答案。只要有一个起点,就很容易了。

我得到了大部分代码,其中只有两篇文章,都是从2010年开始的。
然后我又回到了字体颜色,因为分开比较麻烦

三个小问题,都与标签有关

- Font is too narrow
- Text color is too dark
- Label origin is wrong
默认字体是已知的,所以这是第一个

label.font = [UIFont boldSystemFontOfSize:17.0];
颜色是下一个,因为这很容易。为此使用了图像编辑器的滴管工具

label.textColor = [UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1];
// Is there a difference between alpha:1 and alpha:1.000?
然后是最难的部分。一个接近的猜测,然后一些调整为一个完美的匹配

label.frame = CGRectMake(54, 4, headerView.frame.size.width-20, 22);
现在我们有了一个与当前分组头完全匹配的自定义实现

完成代码:

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

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(54, 4, labelSize.width, labelSize.height)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:[UIFont boldSystemFontOfSize:17.0]];
    [label setShadowColor:[UIColor whiteColor]];
    [label setShadowOffset:CGSizeMake(0, 1)];
    [label setText:[self tableView:tableView titleForHeaderInSection:section]];
    [label setTextColor:[UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1.000]];
    [headerView addSubview:label];

    return headerView;
}
在自己找到合适的字体/颜色后找到。哦,好吧

编辑:

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

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(54, 4, labelSize.width, labelSize.height)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:[UIFont boldSystemFontOfSize:17.0]];
    [label setShadowColor:[UIColor whiteColor]];
    [label setShadowOffset:CGSizeMake(0, 1)];
    [label setText:[self tableView:tableView titleForHeaderInSection:section]];
    [label setTextColor:[UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1.000]];
    [headerView addSubview:label];

    return headerView;
}
对于允许有效无限量文本的标题标签:

// before label init
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
NSUInteger maxWidth = headerView.frame.size.width-108;
CGSize labelSize = [title sizeWithFont:[UIFont systemFontOfSize:17.0]
                     constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
if (labelSize.width < maxWidth) labelSize.width = maxWidth;

// after setFont:
[label setNumberOfLines:0];
//在标签初始化之前
NSString*title=[self tableView:tableView titleForHeaderInSection:section];
NSU整数maxWidth=headerView.frame.size.width-108;
CGSize labelSize=[title sizeWithFont:[UIFont systemFontOfSize:17.0]
constrainedToSize:CGSizeMake(最大宽度,CGFLOAT_MAX)];
如果(labelSize.width
我认为应该有[label release]和[headerView autorelease],除非使用ARC。请不要以破坏我的像素完美自定义标题的方式编辑它。//另外,我注意到,虽然默认标题可以处理过多的标题长度(它会换行到第二行),但这个示例不会显示多行文本,尽管标题视图的高度确实会增加。修复此错误将不胜感激。