How to add a footer to the UITableView?

IphoneUitableviewUiviewFooter

Iphone Problem Overview


I'me using this code to add a footer to the TableView. It has 20 sections, and each section a few rows. There's a titleForHeaderInSection, and sectionForSectionIndexTitle methods.

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];

What am I doing wrong?

thanks,

RL

Iphone Solutions


Solution 1 - Iphone

I'm specifically seeing in my code that

self.theTable.tableFooterView = tableFooter;

works and

[self.theTable.tableFooterView addSubview:tableFooter];

does not work. So stick to the former and look elsewhere for the possible bug. HTH

Solution 2 - Iphone

You need to implement the UITableViewDelegate method

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

and return the desired view (e.g. a UILabel with the text you'd like in the footer) for the appropriate section of the table.

Solution 3 - Iphone

I used that and it worked Perfectly :)

    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
    [footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ProductCellBackground.png"]]];
    self.tableView.tableFooterView = footerView;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    [self.tableView setContentInset:(UIEdgeInsetsMake(0, 0, -500, 0))];

Solution 4 - Iphone

I know that this is a pretty old question but I've just met same issue. I don't know exactly why but it seems that tableFooterView can be only an instance of UIView (not "kind of" but "is member")... So in my case I've created new UIView object (for example wrapperView) and add my custom subview to it... In your case, chamge your code from:

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];

to:

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UIView *wrapperView = [[UIView alloc] initWithFrame:footerRect];

UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";

[wrapperView addSubview:tableFooter];

self.theTable.tableFooterView = wrapperView;
[wrapperView release];
[tableFooter release];

Hope it helps. It works for me.

Solution 5 - Iphone

Initially I was just trying the method:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

but after using this along with:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

problem was solved. Sample Program-

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
	UIView *sampleView = [[UIView alloc] init];
	sampleView.frame = CGRectMake(SCREEN_WIDTH/2, 5, 60, 4);
	sampleView.backgroundColor = [UIColor blackColor];
	return sampleView;
}

and include UITableViewDelegate protocol.

@interface TestViewController : UIViewController <UITableViewDelegate>

Solution 6 - Iphone

These samples work well. You can check section and then return a height to show or hide section. Don't forget to extend your viewcontroller from UITableViewDelegate.

Objective-C

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0)
    {
        // to hide footer for section 0 
        return 0.0;
    }
    else
    {
        // show footer for every section except section 0 
        return HEIGHT_YOU_WANT;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor blackColor];
    return footerView;
}

Swift

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    footerView.backgroundColor = UIColor.black
    return footerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if section == 0 {
        // to hide footer for section 0
        return 0.0
    } else {
        // show footer for every section except section 0
        return HEIGHT_YOU_WANT
    }
}

Solution 7 - Iphone

Swift 2.1.1 below works:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = UIColor.RGB(53, 60, 62)
        return v
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 80
    }

If use self.theTable.tableFooterView = tableFooter there is a space between last row and tableFooterView.

Solution 8 - Iphone

[self.tableView setTableFooterView:footerView];

Solution 9 - Iphone

instead of

self.theTable.tableFooterView = tableFooter;

try

[self.theTable.tableFooterView addSubview:tableFooter];

Solution 10 - Iphone

I had the same problem but I replaced the following line in my header:

@interface MyController : UIViewTableViewController <UITableViewDelegate, UITableViewDataSource>

with this line and it works as expected:

@interface RequestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Notice the UIViewController. Good luck :)

Solution 11 - Iphone

If you don't prefer the sticky bottom effect i would put it in viewDidLoad() https://stackoverflow.com/a/38176479/4127670

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRui LopesView Question on Stackoverflow
Solution 1 - IphonekrisView Answer on Stackoverflow
Solution 2 - IphoneErikView Answer on Stackoverflow
Solution 3 - Iphonepawelini1View Answer on Stackoverflow
Solution 4 - IphonezioolekView Answer on Stackoverflow
Solution 5 - IphoneSubham93View Answer on Stackoverflow
Solution 6 - IphoneabdullahselekView Answer on Stackoverflow
Solution 7 - IphoneWilliam HuView Answer on Stackoverflow
Solution 8 - Iphoneuser3693546View Answer on Stackoverflow
Solution 9 - IphoneamattnView Answer on Stackoverflow
Solution 10 - IphonePowerAktarView Answer on Stackoverflow
Solution 11 - IphonespaceMonkeyView Answer on Stackoverflow