el.severo
el.severo

Reputation: 2287

Show tableView header only when have rows in sections

My Table has two sections and at the first section I don't have elements all the time in it;

I've placed this condition: if([tableView numberOfRowsInSection:section]<=0) return 0; into my viewForHeaderInSection method but my app crases and brings EXC_BAD_ACCESS

Here's my customized headerView:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    if([myTableView numberOfRowsInSection:section]<=0) return 0;

    NSString *headerTitle = [titlesArray objectAtIndex:section];
    UIView *customView = [ [ [UIView alloc] initWithFrame: CGRectMake(0.0, -5.0, 320.0, 25.0) ] autorelease ];
    [customView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myTableBackground.png"]]];
    UIView *clearView = [ [ [ UIView alloc ] initWithFrame: CGRectMake(0.0, -5.0, 320.0, 25.0) ] autorelease ];
    [clearView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:nil]]];
    UIButton *button = [ [ [ UIButton alloc ] initWithFrame: CGRectMake(-9.0, 5.0, 320, 27.0) ] autorelease ];
    button.tag = section;
    [button setImage:[UIImage imageNamed:@"header_section_background.png" ] forState: UIControlStateNormal ];
    button.highlighted = NO;
    button.adjustsImageWhenHighlighted = NO;
    UILabel *label = [ [ UILabel alloc ] initWithFrame:CGRectMake(15.0, 5.0, 45, 22.0) ];
    label.alpha = 1.0;
    label.backgroundColor = [UIColor clearColor];

    label.text = title;
    label.textAlignment = UITextAlignmentRight;

    [customView addSubview: button ];
    [customView addSubview:label ];
    [customView sendSubviewToBack:button];

    return customView;
}

Any clue how to solve this?

Upvotes: 0

Views: 696

Answers (1)

Kristofer Sommestad
Kristofer Sommestad

Reputation: 3061

You should return nil instead of 0.

And you might/should use the tableView parameter instead of your myTableView variable.

Upvotes: 2

Related Questions