Hiding UITableView footer

12,558

Solution 1

Ok here's how I solved this problem.

- (void)refreshTableFooterButton
{
    if (should be visible) {
        self.tableView.tableFooterView = self.tableFooterButton;
    } else {
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
}

I used [[UIView alloc] initWithFrame:CGRectZero instead of nil to prevent unwanted empty cells to appear in the tableview.

I didn't need an animation block, but other people might.

[UIView animateWithDuration:0.5 animations:^{

}];

Also, i had to keep a strong reference to my tableFooterButton IBOutlet. That was part of why my initial attempt at solving the problem failed. I'm not sure if having a strong reference to an IBOutlet is good practice though. Feel free to leave a comment about that.

Solution 2

using the following code :

self.tableView.tableFooterView?.hidden = false

Solution 3

This should do the trick. There are other alternatives such as this answer here.

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0;


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

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}`

Solution 4

use following methods :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}`
Share:
12,558
user3250560
Author by

user3250560

Updated on June 04, 2022

Comments

  • user3250560
    user3250560 almost 2 years

    I am unable to hide my UITableView footer (i.e. setting it's height to 0 and animating the transition).

    I tried to wrap tableView.tableViewFooter.height = 0 (and tableView.tableViewFooter = nil) between [tableView beginUpdates] and [tableView endUpdates] but it doesn't work.

    Implementing the method below creates another footer.

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

    enter image description here

    Is there a difference between a tableview footer and a section footer? I created mine by dropping a button under my tableview in my storyboard.

    Any ideas to do this simply?

  • user3250560
    user3250560 about 10 years
    Is there a difference between the tableview footer and a section footer? Because using this method creates another footer.
  • P. Sami
    P. Sami about 10 years
    No there's no difference. This footer should be really unnoticeable, could you provide a screenshot?
  • P. Sami
    P. Sami about 10 years
    Also try setting it to 0 and see how it goes.
  • user3250560
    user3250560 about 10 years
    I do not have enough reputation to post an image here but here it is i59.tinypic.com/ivvbt3.png with a return value of 50.
  • P. Sami
    P. Sami about 10 years
    Now i've got a question from you, do you want to have a footer?
  • P. Sami
    P. Sami about 10 years
    Do you need some view showing up at the bottom of your table? if that's the case, you just need to have that 50 value for the last section number and you can do the check inside your heightForFooterInSection method.
  • user3250560
    user3250560 about 10 years
    I tried adding an additional row but my code gets quite complicated. I figured that using a footer would be a simpler way to add a button the the bottom of my tableview. More rows are loaded when the user hits this button.
  • user3250560
    user3250560 about 10 years
    Problem with using a section footer is that the footer is always visible on screen. I really want it only to appear when the user scrolls to the bottom of the tableview.
  • P. Sami
    P. Sami about 10 years
    Why don't you add a toolbar instead and have your button there? UIBarButtonItem is what you need.
  • P. Sami
    P. Sami about 10 years
    If you only have the section footer for the last section it would behave the way you want
  • user3250560
    user3250560 about 10 years
    I'm already using a toolbar, it contains different controls. What i need is a footer that behaves like it's the last row of the table. Theres only 1 section so i thought it would behave like its only for the last section.
  • user3250560
    user3250560 about 10 years
    I'm really starting to think that the button is not in a footer but rather in a view below the tableview.
  • P. Sami
    P. Sami about 10 years
    That one looks like a translucent toolbar with a barbuttonitem in the middle.
  • user3250560
    user3250560 about 10 years
    It doesn't hide the button frame. Its only like the text color is set to white. You see what i mean? Also, thx for your time :)
  • user3250560
    user3250560 about 10 years
    :) i saw your edit but... >:D if you add more row, say 20, you'll see that the button is still there, it's frame is still there. I want its frame to be of height 0.
  • P. Sami
    P. Sami about 10 years
    I think that's what you need :) see my edit @user3250560
  • user3250560
    user3250560 about 10 years
    Oh nice! That works but... only once, in viewDidLoad for example. I'd like to be able to resize it by calling a method at runtime and to see the transition animate. hehe
  • P. Sami
    P. Sami about 10 years
    i'll give you a better answer in a couple of minutes! :)
  • user3250560
    user3250560 about 10 years
    Thx very much for the answer! I'll check it out today if I have the time, or else next week.
  • user3250560
    user3250560 about 10 years
    Ok i finally solved this problem. Thank you very much for all your time. I didn't know about animateWithDuration, that hinted me as how to solve this problem. See my answer to know how I did it.
  • jackslash
    jackslash about 10 years
    While having a strong reference to an IBOutlet is not the standard pattern it is ok to hold a strong reference in this case as you are presumably going to put the same view back in the view hierarchy again later. The strong reference avoids having to recreate the view somehow.
  • Rotem
    Rotem over 8 years
    ding ding ding we have a winner