8

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?

Daniyar
  • 2,975
  • 2
  • 26
  • 39
user3250560
  • 646
  • 2
  • 9
  • 15
  • Use tableview delegate method & set height 0 there. or Put some piece of code. – Nico Mar 05 '14 at 19:29
  • @Nico Is there a difference between the tableview footer and a section footer? Because using this method creates another footer. – user3250560 Mar 05 '14 at 20:19

6 Answers6

9

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.

user3250560
  • 646
  • 2
  • 9
  • 15
  • 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. – jackslash Mar 10 '14 at 13:59
8

using the following code :

self.tableView.tableFooterView?.hidden = false
machine
  • 481
  • 5
  • 4
2

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];
}`
Community
  • 1
  • 1
ridacl
  • 21
  • 2
1

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];
}`
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
0

So in the end here's the simplest solution I can give you. So as you can see I just put the .m (for simplicity).

There is a difference between table footer and section footer. What you're looking for is the section footer.

I also added an animation block for adding and removing it.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

UIButton *_footerButton;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createAndAddFooterButton];
}

-(void) buttonPressed:(id)button
{
    [UIView animateWithDuration:2 animations:^{
        self.tableView.tableFooterView = nil;
    }];
}

- (void)createAndAddFooterButton
{
    // here you create the button
    _footerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_footerButton setTitle:@"Button Title" forState:UIControlStateNormal];
    [_footerButton sizeToFit];
    [_footerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [UIView animateWithDuration:2 animations:^{
        self.tableView.tableFooterView = _footerButton;
    }];
}

@end
P. Sami
  • 2,115
  • 2
  • 21
  • 39
  • Is there a difference between the tableview footer and a section footer? Because using this method creates another footer. – user3250560 Mar 05 '14 at 20:18
  • No there's no difference. This footer should be really unnoticeable, could you provide a screenshot? – P. Sami Mar 05 '14 at 20:27
  • Also try setting it to 0 and see how it goes. – P. Sami Mar 05 '14 at 20:28
  • I do not have enough reputation to post an image here but here it is http://i59.tinypic.com/ivvbt3.png with a return value of 50. – user3250560 Mar 05 '14 at 20:41
  • Now i've got a question from you, do you want to have a footer? – P. Sami Mar 05 '14 at 20:45
  • 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. – P. Sami Mar 05 '14 at 20:46
  • 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 Mar 05 '14 at 20:48
  • 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. – user3250560 Mar 05 '14 at 20:49
  • Why don't you add a toolbar instead and have your button there? UIBarButtonItem is what you need. – P. Sami Mar 05 '14 at 20:50
  • If you only have the section footer for the last section it would behave the way you want – P. Sami Mar 05 '14 at 20:51
  • 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 Mar 05 '14 at 20:54
  • I'm really starting to think that the button is not in a footer but rather in a view below the tableview. – user3250560 Mar 05 '14 at 20:57
  • That one looks like a translucent toolbar with a barbuttonitem in the middle. – P. Sami Mar 05 '14 at 21:01
  • 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 Mar 05 '14 at 21:31
  • :) 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. – user3250560 Mar 05 '14 at 21:49
  • I think that's what you need :) see my edit @user3250560 – P. Sami Mar 05 '14 at 22:02
  • 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 – user3250560 Mar 06 '14 at 13:55
  • i'll give you a better answer in a couple of minutes! :) – P. Sami Mar 06 '14 at 14:17
  • Thx very much for the answer! I'll check it out today if I have the time, or else next week. – user3250560 Mar 07 '14 at 15:45
  • 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. – user3250560 Mar 10 '14 at 13:55
0

My problem was that the clear line was the footer which I could only define as 1 pixel high and not 0 (as I do not want to have a footer). The only way I could fix it was to provide a view in viewForFooterInSection and set the background colour to the same as the header, so that visually you don't notice, and when I animate-in new rows they do not show through the top of the header where the clear line (footer) was.

    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let blankView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 1))
    blankView.backgroundColor = {Same colour as Header}
    return blankView
ziggyzoggy
  • 153
  • 1
  • 8