24

I have been working to hide the footerview for while. My problem is I have a button in footer when I click the button one section will be added below as the last section and the button too will shift to the newly created section and now I want to hide the footer in the previous section of the table after the update of sections.

footerView.hidden = YES

I used this in the button action but its not working.

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
New Xcoder
  • 535
  • 1
  • 4
  • 14

8 Answers8

47

There are four solutions. They are,

Solution 1:

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];
}

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

Solution 2:

You can set the footer/header height via interface builder under the size tab.

Solution 3:

set contentInset property.

self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);

It is used to make the top and bottom touch the edge.

Solution 4:

implement the below, set the values as per your condition. 0.0 will not be accepted. The lower value should be 1.0.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
    if(section == 0) {
       return 6;
    } else {
       return 1.0;
    }
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • 3
    @@Ramshad You should reference or attribute the work you copy off. Also, specifying 0.00001f results in zero height. http://stackoverflow.com/questions/2817308/reducing-the-space-between-sections-of-the-uitableview – Matt Aug 28 '14 at 05:33
18

This should do it

tableView.tableFooterView.hidden = YES;

In Swift 5:

 tableView.tableFooterView = nil
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
shabbirv
  • 8,958
  • 4
  • 33
  • 34
  • 1
    I don't think you're trying to hide the "footerView" then, but rather the last cell of the section. My answer correctly shows how to hide the footerView – shabbirv Jul 12 '12 at 05:26
  • 1
    Its not hiding the footerview man . Next section pulls after the footerview . – New Xcoder Jul 12 '12 at 05:28
  • 8
    There are two kinds of footer views: tableFooterView and sectionFooterView. Hence the confusion... – Rok Jarc Apr 08 '14 at 08:52
  • 1
    To make it work you should do this, `- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { [tableView.tableFooterView setHidden:YES]; return 1.0f; }` **Key point is return 1.0f** – Hwangho Kim Dec 16 '15 at 13:35
  • This does not work. Section footers are not hidden and are not even reduced to 0 height. – hasen Feb 13 '18 at 02:27
9

This can do it by implementing delegate method

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return CGFLOAT_MIN;
}
J.Hong
  • 91
  • 1
  • 1
5

Referring to @J.Hong answer, Swift4 version (iOS 11.3):

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNonzeroMagnitude
}

Comparing to @Tim Newton answer:

  • there is no need to call titleForFooterInSection
  • simplify CGFloat.leastNormalMagnitude -> .leastNonzeroMagnitude (more Swifty IMO)
Kamil Harasimowicz
  • 4,684
  • 5
  • 32
  • 58
4

Swift 3.0 solution that also removes the pesky 1px border between the previous cell and the following header.

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return nil
}
Tim Newton
  • 1,463
  • 1
  • 13
  • 13
  • I have to show/hide the footer based on a HTTP request. How I can apply changes? Because your example is executed initially, I guess. – JCarlosR May 08 '17 at 02:23
3

in swift4:

self.tableView.tableFooterView = nil
Mohamad.j
  • 79
  • 1
  • 4
0

When not using custom views, return nil from tableView:titleForFooterInSection: and 0 from tableView:heightForFooterInSection:.

@property(nonatomic, strong) NSString *footerTitle;
@property(nonatomic, assign) BOOL shouldHideFooter;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return self.shouldHideFooter ? nil : self.footerTitle;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.shouldHideFooter ? 0 : UITableViewAutomaticDimension;
}
bteapot
  • 1,897
  • 16
  • 24
0

Swift 5 solution: (hides section footers, you may need another piece to get rid of the table footer)

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.0
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView()
}
Dylan Reich
  • 1,400
  • 2
  • 11
  • 14