ios - 自定义标题与组中的单元格重叠

标签 ios objective-c uitableview ios5

我在尝试创建自定义 iOS 表格标题时遇到了困难。我的标题是 30px 高,我使用这段代码来创建它们:

- (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;
} 

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

custom headers

任何人都可以指出我在此处清理 header 的正确方向吗?

提前致谢

最佳答案

你缺少一个[空格]:

你有:

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

应该是:

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

关于ios - 自定义标题与组中的单元格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10359453/

相关文章:

ios - 带行高的 Three20 框架?

ios - 无法从枚举 block 将 ALAssets 添加到 NSOperationQueue

iphone - 与 interactivePopGestureRecognizer 一起调整位置

performance - UITableView 加载更多内容而无需重新加载完整表格

ios - AFNetworking setImageWithURL :placeholderImage: wrong size

ios - 如何指定WKWebView请求的超时间隔?

ios - Swift - 从视频中获取所有帧

ios - swift3 日期到数据,数据到日期转换

ios - UITextField 文本未保存

ios - UIScrollView 内的 UITableView 在滚动后没有收到第一次点击