Static footer in iOS

15,017

A UITableView's tableFooterViewproperty is a UIViewobject that is always displayed at the bottom of the content, below the last section. Even if that is not really clear in Apple's documentation (extract : "Returns an accessory view that is displayed below the table.").

If you want the footer to be static and non-floating, you have two easy choices :

  • Not ideal but simple : Use the last section's footer view as your static footer. This will work at some conditions :
    • your UITableView style must be UITableViewStylePlain (as UITableViewStyleGroupedgives the same behaviour to section headers/footers as to the UITableView tableHeaderView and tableFooterView
    • You won't be able to use the last section footer for its real purpose : giving footer informations to a specific section

Here is a simple example :

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = nil;
    if (section == [tableView numberOfSections] - 1) {
        // This UIView will only be created for the last section of your UITableView
        view = [[UIView alloc] initWithFrame:CGRectZero];
        [view setBackgroundColor:[UIColor redColor]];
    }
    return view;
}
  • Best solution right now : Add a UIView (either in code or in your XIB) at the same level as your UITableView. One little condition :
    • The self.view property of your UIViewController must not be your UITableView object. That means you can't subclass UITableViewController but UIViewController and make your controller conform to UITableViewDataSource and UITableViewDelegate protocols. It's actually simpler than it looks and a better implementation (as far as I'm concerned) than using directly UITableViewController.

Here is a simple example in code (but you can do exactly the same using Interface Builder) :

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

ViewController.m :

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *fixedTableFooterView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat fixedFooterHeight = 200.0;

    // Initialize the UITableView
    CGRect tableViewFrame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - fixedFooterHeight);
    self.tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain]; // or Grouped if you want...
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.view addSubview:self.tableView];

    // Initialize your Footer
    CGRect footerFrame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - fixedFooterHeight, CGRectGetWidth(self.view.bounds), fixedFooterHeight); // What ever frame you want
    self.fixedTableFooterView = [[UIView alloc] initWithFrame:footerFrame];
    [self.fixedTableFooterView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.fixedTableFooterView];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    [self setTableView:nil];
    [self setFixedTableFooterView:nil];
}

@end

You can also specify UIViewAutoresizing mask to make it work seamlessly in portrait and landscape but I didn't to complicate this rather simple code.

WARNING : This .h and .m files won't compile as I didn't put in the UITableViewDataSource required methods. Comment the setDataSource: line if you want to see it in action.

Hope this will help,

Feel free to ask me other details,

Share:
15,017

Related videos on Youtube

user2575929
Author by

user2575929

Updated on September 16, 2022

Comments

  • user2575929
    user2575929 over 1 year

    Im Looking to get my footer to Float over a table view So that it is Always Visible and not only when i scroll to the bottom.

    i have tried resizing the table view using this code :

    - (id)initWithStyle:(UITableViewStyle)style
    {
        if ((self = [super initWithStyle: style]))
        {
            UIView *footer = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 1, 45)];
            footer.backgroundColor = [UIColor clearColor];
            self.tableView.tableFooterView = footer;
            self.tableView.frame = CGRectMake(0, 0, 320, 100);
            footer.hidden = NO;
    
        }
    

    I have had no luck thus far.

    I have also tried Using other windows and resizing all the .XIB files.

  • Adeel
    Adeel about 8 years
    Yes but its possible in case of UIViewController only. If my view is UITableViewController then ?
  • Vojtech Vrbka
    Vojtech Vrbka about 8 years
    If you need lot of customisation, just add UITableView to UIViewController instead of using UITableViewController
  • Adeel
    Adeel about 8 years
    Yes This seems to be ultimate solution to support all the required features in a seamless manner. Any way thanks dear :)
  • MhmdRizk
    MhmdRizk over 5 years
    there still remains 1 issue in this solution, that I want to use static cells in the table view, which can be used in a UITableView, without using UITableViewController.