UITableView tableFooterView not appearing

11,768

By looking at the header file of UITableView.h we see the declaration of property tableFooterView like this:

@property(nonatomic,retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer

so the default property is nil. That's why you can't add another UIView to nil UIView. You should do something like this:

controls.tableFooterView = myFooterView;
Share:
11,768
Armand
Author by

Armand

I have been a professional software developer for 11 years. I mostly work with .NET based technologies building web applications with various JavaScript frameworks. I love working on new and exiting projects irrespective of the industry the project is built for. Besides programming I am an avid sport shooter, shooting in IDPA and IPSC competitions.

Updated on June 13, 2022

Comments

  • Armand
    Armand almost 2 years

    I have a UITableView to which I add a tableFooterView, for some reason the tableFooterView is not appearing?

    How I add my tableFooterView

    I add the tableFooterView in a connectionDidFinishLoading method after reloading the tableview data.

    So what I do is

    [controls reloadData];
    
    UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    if(self.item.canRunOnDemand)
    {
        UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonRunWorkflow addTarget:self action:@selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
        [buttonRunWorkflow setTitle:@"Run Now" forState:UIControlStateNormal];
        buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44); 
        buttonRunWorkflow.backgroundColor = [UIColor clearColor];
        [buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [myFooterView addSubview:buttonRunWorkflow];
    }
    if(item.canRunAlways)
    {
        UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
        canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
        canRunAlwaysLabel.text = @"Run Always:";
        UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
        [canRunAlways addTarget:self action:@selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
        [myFooterView addSubview:canRunAlways];
        [myFooterView addSubview:canRunAlwaysLabel];
        [canRunAlwaysLabel release];
        [canRunAlways release];
    }
    
    [myFooterView release];
    
    [controls.tableFooterView addSubview:myFooterView];
    

    Footer view height

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        return 100;
    }
    

    I have also tried this:

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        if(section == [[fields objectAtIndex:section] count] - 1)
        {
            return 100;
        }
        else {
            return 0;
        }
    }
    -(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        if(section == [[fields objectAtIndex:section] count] - 1)
        {
            UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
            if(self.item.canRunOnDemand)
            {
                UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [buttonRunWorkflow addTarget:self action:@selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
                [buttonRunWorkflow setTitle:@"Run Now" forState:UIControlStateNormal];
                buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44); 
                buttonRunWorkflow.backgroundColor = [UIColor clearColor];
                [buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [myFooterView addSubview:buttonRunWorkflow];
            }
            if(item.canRunAlways)
            {
                UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
                canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
                canRunAlwaysLabel.text = @"Run Always:";
                UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
                [canRunAlways addTarget:self action:@selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
                [myFooterView addSubview:canRunAlways];
                [myFooterView addSubview:canRunAlwaysLabel];
                [canRunAlwaysLabel release];
                [canRunAlways release];
            }
            return myFooterView;
        }
        else {
            return nil;
        }
    }
    
  • Armand
    Armand about 12 years
    But what happens when the table is empty?
  • Regexident
    Regexident over 9 years
    You're confusing "section footer views" with "table footer views" here. OP was asking for the latter.
  • Zorayr
    Zorayr over 8 years
    This is incorrect, section footers will float on top of the table view.