UITableViewStyleGrouped默认标题视图?

Thr*_*dyn 2 uitableview

我正在尝试实现一个用于删除整个部分的控件,如果删除按钮位于标题中,它会在我的应用程序中看起来最好,而不是像UIPopoverView这样的叠加.


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

Thr*_*dyn 11

我从这个博客获得了大部分代码,其中只有两个帖子,都来自2010年.
然后我回到这个网站只是为了字体颜色,因为分手更麻烦.

三个小问题,都带有标签.

- Font is too narrow
- Text color is too dark
- Label origin is wrong
Run Code Online (Sandbox Code Playgroud)

默认字体是已知的,因此首先出现.

label.font = [UIFont boldSystemFontOfSize:17.0];
Run Code Online (Sandbox Code Playgroud)

颜色是下一个,因为这很容易.使用图像编辑器的吸管工具.

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?
Run Code Online (Sandbox Code Playgroud)

然后是困难的部分.仔细猜测,然后进行一些完美匹配的调整.

label.frame = CGRectMake(54, 4, headerView.frame.size.width-20, 22);
Run Code Online (Sandbox Code Playgroud)

现在我们有一个完全匹配当前Grouped标头的自定义实现.

完成的代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

发现这个苏答案找到合适的字体后/ COLOR自己.那好吧.

编辑:

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

// 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];
Run Code Online (Sandbox Code Playgroud)