36

Is there any way to reload the section header/footer of a table view without calling [tableView reloadData];?

In fact, I want to show the number of cells in a table view's section in its section footer. The table view is editable and I delete or insert rows using

– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:

It seems that these methods do not update the section footer. Strangely, when I call these methods the table view data-source method

- (NSString *)tableView:(UITableView *)table titleForFooterInSection:(NSInteger)section 

is called (twice!) but it does not update the table view with the new values!!

Anyone has any idea how to fix this problem?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ali
  • 829
  • 1
  • 8
  • 9

11 Answers11

36

If you just have a basic text header or footer, you can access them directly:

[tableView footerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForFooterInSection:indexPath.section];

similarly for the header:

[tableView headerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForHeaderInSection:indexPath.section];
Nico teWinkel
  • 870
  • 11
  • 19
  • 4
    But accessing the `textLabel` directly doesn't change the frame to fit the new text. Do I have to call `sizeToFit` on the footer/header view? – Blip May 27 '15 at 00:03
  • You'll need to call beginUpdates/endUpdates to have the table view recalculate its layout – Vadim Yelagin Jun 14 '15 at 08:26
  • Gold! tableView.headerViewForSection(indexPath.section)?.textLabel.text = tableView(tableView, titleForHeaderInSection: indexPath.section) – DogCoffee Aug 08 '15 at 13:13
  • Note that this doesn't respect the styling of the OS. Such as the all uppercase font that is applied to grouped section headers. – Ben Guild Sep 11 '16 at 04:33
25

This should work:

    UIView.setAnimationsEnabled(false)
    tableView.beginUpdates()

    if let containerView = tableView.footerViewForSection(0) {
        containerView.textLabel!.text = "New footer text!"

        containerView.sizeToFit()
    }

    tableView.endUpdates()
    UIView.setAnimationsEnabled(true)
Craig Miller
  • 543
  • 8
  • 9
  • It does work pretty fine in iOS 9, Swift 2.2. And it's much more cleaner than harcoding frame rectangles, which should certainly be avoided. – maganap Jun 07 '16 at 08:22
8

I managed to do it in an indirect way: I created a UILabel and set it as section header/footer.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    // update sectionFooterView.text    
    return sectionFooterView;
}

- (void)viewDidLoad {
    // create sectionFooterView in Interface Builder, change the frame here
    // use Font-size:15 , BG-Color: clearColor , text-Color: RGB=(97,105,118) 
    // and Text-alignment:Center to look like table view's original footer
    sectionFooterView.frame = CGRectMake(0, 10, 320, 12);
}

Does anyone know a way to do this without setting a custom footer view?

Ali
  • 829
  • 1
  • 8
  • 9
6

This is how you do it in Swift 3.0

tableView.beginUpdates()
tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text"
tableView.endUpdates()
Kevin R
  • 8,230
  • 4
  • 41
  • 46
Daniel Tovesson
  • 2,550
  • 1
  • 30
  • 41
3

Here's another way to do it:

UITableViewHeaderFooterView *headerView=[self.tableView headerViewForSection:1];
CATransition *animation = [CATransition animation];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionFade;
animation.duration = 0.35;
[headerView.textLabel.layer addAnimation:animation forKey:@"kCATransitionFade"];
headerView.textLabel.text=[self tableView:self.tableView titleForHeaderInSection:1];
[headerView.textLabel sizeToFit];
hyperspasm
  • 1,243
  • 1
  • 16
  • 27
2

Here's a UITableView extension that's a handy shortcut for refreshing the tableView header/footer titles, based on some of the answers above (note the artificial uppercasing of the header title).

extension UITableView {

    func refreshHeaderTitle(inSection section: Int) {
        UIView.setAnimationsEnabled(false)
        beginUpdates()

        let headerView = self.headerView(forSection: section)
        headerView?.textLabel?.text = self.dataSource?.tableView?(self, titleForHeaderInSection: section)?.uppercased()
        headerView?.sizeToFit()

        endUpdates()
        UIView.setAnimationsEnabled(true)
    }

    func refreshFooterTitle(inSection section: Int) {
        UIView.setAnimationsEnabled(false)
        beginUpdates()

        let footerView = self.footerView(forSection: section)
        footerView?.textLabel?.text = self.dataSource?.tableView?(self, titleForFooterInSection: section)
        footerView?.sizeToFit()

        endUpdates()
        UIView.setAnimationsEnabled(true)
    }

    func refreshAllHeaderAndFooterTitles() {
        for section in 0..<self.numberOfSections {
            refreshHeaderTitle(inSection: section)
            refreshFooterTitle(inSection: section)
        }
    }
}
Loz
  • 2,198
  • 2
  • 21
  • 22
2

Check the documentation for UITableView, or more specifically -reloadSections:withRowAnimation:

Tim
  • 597
  • 1
  • 5
  • 15
  • This works but it's not the most efficient since it won't just update the section header/footer, but will also update all the cells. Seems like Ali's answer is the way to go. – Adrian Schönig Jan 31 '12 at 08:24
  • 2
    I think that apple expects that if you change the footer or header, the content will change too. But yes, if you want to customize much more than just text displaying semi-statically, then using viewForFooter/Header can be easier. Just be aware that the default section headers and footers do a lot of formatting for you, and overriding them like this for something trivial may also not be the best option. – Tim Feb 02 '12 at 22:01
0

I have found that the footer text doesn't update correctly if the text changes from filling one line to multiple ones back to one line. Reseting the label width and height would solve this issue.

    UIView.setAnimationsEnabled(false)
    textLabel.frame = CGRect(x: textLabel.frame.minX,
                             y: textLabel.frame.minY,
                             width: 0,
                             height: 0)
    tableView.beginUpdates()
    textLabel.text = "Your text"
    textLabel.sizeToFit()
    tableView.endUpdates()
    UIView.setAnimationsEnabled(true)
mukaissi
  • 2,441
  • 1
  • 21
  • 12
0

It worked for me to just reload the section with reloadSections on my table view

andre
  • 88
  • 2
  • 5
0
self.controller?.reloadData()

Because footer is part of the whole controller and not under a tableView. We have to reload the whole controller for the FooterView to get updated.

GOKuLkrish
  • 11
  • 3
-2

You can also do it this way

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    switch section {
        case 0:
            return "Some string"
        default:
            return ""
    }
}