0

I have several Sections in my UITableView which sometimes can have 0 rows in it. How can i remove section header if the numberOfRowsInSection is equal to 0?

Here is my code for the sections:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, tableView.frame.size.width, 22)];
    [label setFont:[UIFont boldSystemFontOfSize:14]];
    [label setTextColor:[UIColor whiteColor]];
    NSString *string =[[theLeagueArray objectAtIndex:section] objectForKey:@"league"];
    [label setText:string];
    [view addSubview:label];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 22)];
    [imgView setImage:[UIImage imageNamed:[[theLeagueArray objectAtIndex:section] objectForKey:@"img"]]];
    [view addSubview:imgView];

    [view setBackgroundColor:[UIColor colorWithRed:13/255.0f green:78/255.0f blue:102/255.0f alpha:1.0f]];

    return view;
}
user3258468
  • 354
  • 7
  • 18

4 Answers4

0

Try deleteSections:withRowAnimation: method. This will remove it from the view, with an optional animation, without affecting your backing data. (You should, however, update the data anyway so that the section doesn't reappear.)

Please check UITableView class reference

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
0
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // If array count 0
        if([[workoutarray objectAtIndex:section] count]==0)
        {
            return nil;
        }
        else {


        }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
     // If array count 0
    if([[workoutarray objectAtIndex:section] count]==0)
        {
            return 0;
        }
        else
        {
            return 25;
        }        
}
Sandeep Agrawal
  • 425
  • 3
  • 10
0

Basically, the right way to do it is perform deleteSections:withRowAnimation: on your table view and do not forget to update data source appropriately.

But you also can return UIView with 0.1 height (it's kinda hack), so it will be invisible for user in this case. To update section perform reloadSections:withRowAnimation: method. It will look like this:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ([tableView numberOfRowsInSection:section] == 0) {
        return [[UIView alloc] init];
    }
... 
}
Ossir
  • 3,109
  • 1
  • 34
  • 52
0

Look at the reference documentation for tableView:heightForHeaderInSection: and make it return 0 when you need to hide the section header (also return nil in tableView:viewForHeaderInSection: for less total calculations).

Also if you target iOS 7 you can use the new tableView:estimatedHeightForHeaderInSection: for even more optimization.

Shebuka
  • 3,148
  • 1
  • 26
  • 43