12

I have a UITableView where I want to force a refresh of just a specific section header row, not the data rows. For example, refresh the header for section 3. I know how to reload an entire section (header plus data), but can just the section header be refreshed?

EDIT: When data changes in one section of the table I want to update information in the header of a DIFFERENT section. Therefore, I need to get a reference to the section header and then refresh it.

ChrisP
  • 9,796
  • 21
  • 77
  • 121
  • can u check this trouble dude http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part?noredirect=1#comment51326939_31692692 – Nischal Hada Jul 29 '15 at 06:53

4 Answers4

14

If the header is a custom UIView, then you can just call setNeedsDisplay on the UIView rather than on the UITableView.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • How do you get a reference to the section header custom UIView to call setNeedsDisplay? I know how to get a reference to a specific row, but not the header. – ChrisP Jan 04 '12 at 18:56
  • I believe you can do this even if the header is not a "custom" `UIView` since aren't all headers instances of `UIView`s – tacos_tacos_tacos Jan 04 '12 at 18:57
  • 2
    1 : maintain the header views you created in an array OR 2 : iterate through the UITableView subviews until you find the header with your desired view.tag – NSIntegerMax Jan 04 '12 at 19:38
  • 1
    Interestingly, if you refresh a single section using [tableView reloadSections...] the other custom header views are not redrawn even if you call [customHeaderView setNeedsDisplay]. I had to reload the associated sections to get their header views to redraw. – ChrisP Jan 04 '12 at 20:16
  • 5
    @ChrisP & @NSInterMax: *How do you get a reference to the section header custom UIView to call setNeedsDisplay?* `UIView* sectionHeader = [self tableView:self.tableView viewForHeaderInSection:[indexPath section]];` – SwiftArchitect Jan 27 '14 at 02:52
  • what could we do in case of custom cell in viewForHeaderInSection to reload particular viewForHeaderInSection – Nischal Hada Jul 29 '15 at 05:37
  • can u check this trouble dude http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part?noredirect=1#comment51326939_31692692 – Nischal Hada Jul 29 '15 at 06:53
  • tableView.headerViewForSection(0)?.textLabel.text = tableView(tableView, titleForHeaderInSection: 0) – DogCoffee Aug 08 '15 at 14:05
  • This does not seem to work if you use self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension – lostintranslation Jun 02 '16 at 16:50
9

Well, in my case, after some asynchronous task i need to update my custom section header view.

and I get it to update it's height using : This Swift 3+ Code

//to reload your cell data
self.tableView.reloadData()
DispatchQueue.main.async {
// this is needed to update a specific tableview's headerview layout on main queue otherwise it's won't update perfectly cause reloaddata() is called
  self.tableView.beginUpdates()
  self.tableView.endUpdates()
}        
Rafat touqir Rafsun
  • 2,777
  • 28
  • 24
  • I have no idea why reloadData() doesn't refresh section headers, and it might be a bug, but `beginUpdates() endUpdates()` also worked for me. – Zonily Jame Jun 27 '17 at 10:09
  • This helped me, was having troubles with the last section footer disappearing after reloadData – Dan Sep 13 '17 at 09:05
  • Doesn't this reload the whole table? The OP explicitly wanted only a single header reloaded. – Tyler A. Jan 10 '19 at 11:06
1

UITableView does *not have any such method to reload only the header view/title ...You have to reload the whole section or the table.

phidah
  • 5,794
  • 6
  • 37
  • 58
Ali3n
  • 1,244
  • 6
  • 10
  • can u check this trouble dude http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part?noredirect=1#comment51326939_31692692 – Nischal Hada Jul 29 '15 at 06:53
-4

I beg to differ. I've run into this while deleting rows with custom section headers and (at least in iOS 6) you can simply call:

[self tableView:self.tableView viewForHeaderInSection:[indexPath section]];

And as long as you have proper code to return a view in that method, you can easily refresh the header for a specific section:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if ([self tableView:tableView numberOfRowsInSection:section] == 0) {

        return nil; //hide the header if there are no rows...

    } else {
        // configure or refresh the header...
    }
}
jhilgert00
  • 5,479
  • 2
  • 38
  • 54
  • Do you know if something like this is possible in UICollectionViews? – NullHypothesis Aug 25 '14 at 13:11
  • This is the correct answer and should be accepted as such. – denvdancsk Dec 11 '14 at 21:39
  • can u check this trouble dude http://stackoverflow.com/questions/31692692/what-could-we-do-in-case-of-custom-cell-in-viewforheaderinsection-to-reload-part?noredirect=1#comment51326939_31692692 – Nischal Hada Jul 29 '15 at 06:53
  • 2
    You are calling the table's delegate method, that's a mistake. You should either reload the table if you only have one section, or reload the individual section if you have multiple. – malhal Feb 14 '16 at 22:00