Felipe Cypriano
Felipe Cypriano

Reputation: 2737

UITableView custom section header appears below the cells

I've a custom section header in my UITableView and I can't figure out why they are appearing bellow the UITableViewCell of the table. See the screenshots:

UITableViewCell are above the section header

This is the code that creates 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];
}

And the section's cell are inserted or deleted when the user touches the section header:

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

How can I make the section header appears above the cells? How to fix it?

Update to show how things are created

These are the class methods I'm using:

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

Upvotes: 1

Views: 3526

Answers (2)

Felipe Cypriano
Felipe Cypriano

Reputation: 2737

I found the problem. I was setting the backgroundColor using alpha (yeah, I can't believe I miss this).

Wrong code in initWithFrame:

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

Correct code:

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

Upvotes: 1

Nava Carmon
Nava Carmon

Reputation: 4533

Try to change your whole table style to be grouped instead of plain. Or change your section view to be opaque. Whatever is the design requirement.

Upvotes: 0

Related Questions