【问题标题】:custom header overlaps cells in group自定义标题与组中的单元格重叠
【发布时间】:2012-05-08 17:20:18
【问题描述】:

我在尝试创建自定义 ios 表头时有点卡住了。我的标题是 30 像素高,我正在使用以下代码来创建它们:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(0, 0, 140, 30);
    label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"header_gradient.png"]];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:12];
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
    [view addSubview:label];

    return view;
}

- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
    return 30;
} 

它几乎可以工作,但我的标题似乎与它们下面的单元格/标题发生冲突:

谁能指出我在此处清理标题的正确方向?

提前致谢

【问题讨论】:

    标签: ios objective-c uitableview ios5


    【解决方案1】:

    您缺少一个 [空格]:

    你有:

    - (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
    

    应该是:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    

    【讨论】:

    • 其实应该是- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    • 正确。正确的声明如下(来自文档): - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section - 如果您密切注意,tableView 和 heightForHeaderInSection 之间缺少 [空格]。 . 第一次完全错过了,因此我认为骆驼案是关闭的。我已经更新了答案。谢谢你的收获!
    • 感谢您的帮助...我快疯了,想弄清楚为什么这不起作用!
    猜你喜欢
    • 2011-02-23
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多