UITableView自定义节标题显示在单元格下方

时间:2010-12-04 18:03:14

标签: iphone objective-c uitableview ios

我在UITableView中有一个自定义节标题,我无法弄清楚它们出现在表格的UITableViewCell之下的原因。请参见屏幕截图:

UITableViewCell are above the section header

这是创建节标题的代码:

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

    return [LojaInfoHeaderView lojaInfoHeaderForSection:section withTitle:sectionTitle opened:[self sectionIsOpen:section] andDelegate:self];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [LojaInfoHeaderView viewHeight];
}

当用户触摸节标题时,插入或删除该节的单元格:

- (void)lojaInfoHeader:(LojaInfoHeaderView *)lojaInfoHeader sectionDidOpen:(NSInteger)section {
    NSArray *indexPathsToInsert = [self indexPathsForSection:section];
    [self setSection:section open:YES];
    [_tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop];
}

- (void)lojaInfoHeader:(LojaInfoHeaderView *)lojaInfoHeader sectionDidClose:(NSInteger)section {
    NSArray *indexPathsToDelete = [self indexPathsForSection:section];
    [self setSection:section open:NO];
    [_tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];    
}

如何使节标题出现在单元格上方?如何解决?

更新以显示内容的创建方式

这些是我正在使用的类方法:

+ (CGFloat)viewHeight {
    return 44.0;
}

+ (LojaInfoHeaderView *)lojaInfoHeaderForSection:(NSInteger)section withTitle:(NSString *)title opened:(BOOL)isOpen andDelegate:(id<LojaInfoHeaderDelegate>)delegate {
    LojaInfoHeaderView *newHeader = [[[LojaInfoHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    newHeader.section = section;
    [newHeader setTitle:title];
    newHeader.delegate = delegate;
    [newHeader setOpen:isOpen animated:NO];
    return newHeader;
}

2 个答案:

答案 0 :(得分:0)

尝试将整个表格样式更改为分组而不是普通。或者将您的剖面视图更改为不透明。无论设计要求是什么。

答案 1 :(得分:0)

我发现了问题。我正在使用alpha设置backgroundColor(是的,我不敢相信我想念这个)。

initWithFrame中的代码错误:

self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];

正确的代码:

self.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.89 alpha:1.0];