132

I've set up the tableview with correct delegate and datasource linkages.. the reloadData method calls the datasource and the delegate methods except for viewForHeaderInSection:.

Why is that so?

mfaani
  • 33,269
  • 19
  • 164
  • 293
inforeqd
  • 3,209
  • 6
  • 32
  • 46

16 Answers16

266

The use of tableView:viewForHeaderInSection: requires that you also implement tableView:heightForHeaderInSection:. This should return an appropriate non-zero height for the header. Also make sure you do not also implement the tableView:titleForHeaderInSection:. You should only use one or the other (viewForHeader or titleForHeader).

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 3
    Make sure there isn't a typo in the method's signature. One wrong letter will mean it won't be called. Check the case too. Also make sure you are returning 0 from `numberOfSections`. – rmaddy Feb 26 '13 at 02:23
  • everything is correct and compiles correctly.. the issue I wanted to understand is around the timing of when the method is called .. tableView:viewForHeaderInSection is called when the table is just about to be displayed and not as part of the synch execution of [tableview reloadData] – inforeqd Feb 26 '13 at 03:03
  • @maddy OMG Thank you, it's so stupid of me i created my instances but I didn't append to my array – Happiehappie Nov 01 '16 at 03:15
  • 5
    You can absolutely have both. viewForHeaderInSection: will have precedence over titleForHeaderInSection: The only requirement is that you set estimatedSectionHeaderHeight on your table view with something different than 0, otherwise viewForHeaderInSection: will never get called – romrom Feb 17 '17 at 14:54
  • Adding to @romrom's comment: if you have implemented both `titleForHeaderInSection:` and `viewForHeaderInSection:` and the view returned from the latter is a subclass of `UITableViewHeaderFooterView` then its `textLabel.text` is automatically set to the all-caps version of the `titleForHeaderInSection:` string. To prevent this behavior, either don't implement `titleForHeaderInSection:` or use a custom label instead of the inherited `textLabel`. – Ortwin Gentz Mar 21 '18 at 15:04
  • I've just encountered a situation where only one of the headers isn't called. The heightForHeaderInSection is called for all sections, but the viewForHeaderInSection is not called for one of the sections. Which one? The one where the included textfield was just edited. Why does it need to be called? Because the editing could cause the section sort to change. When the data is updated, it is sorted, so I want to redisplay ALL sections, including the section just edited. Weirdly, that is the one that is skipped even though height is queried. – Victor Engel Aug 26 '21 at 15:08
43

The trick is that those two methods belong to different UITableView protocols: tableView:titleForHeaderInSection: is a UITableViewDataSource protocol method, where tableView:viewForHeaderInSection belongs to UITableViewDelegate.

That means:

  • If you implement the methods but assign yourself only as the dataSource for the UITableView, your tableView:viewForHeaderInSection implementation will be ignored.

  • tableView:viewForHeaderInSection has a higher priority. If you implement both of the methods and assign yourself as both the dataSource and the delegate for the UITableView, you will return the views for section headers but your tableView:titleForHeaderInSection: will be ignored.

I have also tried removing tableView:heightForHeaderInSection:; it worked fine and didn't seem to affect the procedures above. But the documentation says that it is required for the tableView:viewForHeaderInSection to work correctly; so to be safe it is wise to implement this, as well.

Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
  • 6
    You made my day!!! Forgot to assign ``UITableViewDelegate`` to ``self``, because I thought, that ``tableView:viewForHeaderInSection`` is a ``UITableViewDataSource`` method. Thank you! – denis631 Apr 10 '16 at 08:11
  • 1
    "tableView:viewForHeaderInSection" is not vital. What's vital is you *somehow* return a height. You could achieve that through either 1. estimate or 2. a hardcoded value or 3. a `titleForHeader` which has an intrinsic size. The intrinsic size is calculated based on the Font Family and size. – mfaani Dec 13 '17 at 21:18
29

@rmaddy has misstated the rule, twice: in reality, tableView:viewForHeaderInSection: does not require that you also implement tableView:heightForHeaderInSection:, and also it is perfectly fine to call both titleForHeader and viewForHeader. I will state the rule correctly just for the record:

The rule is simply that viewForHeader will not be called unless you somehow give the header a height. You can do this in any combination of three ways:

  • Implement tableView:heightForHeaderInSection:.

  • Set the table's sectionHeaderHeight.

  • Call titleForHeader (this somehow gives the header a default height if it doesn't otherwise have one).

If you do none of those things, you'll have no headers and viewForHeader won't be called. That's because without a height, the runtime won't know how to resize the view, so it doesn't bother to ask for one.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • From the docs for `tableView:viewForHeaderInSection:`: " This method only works correctly when `tableView:heightForHeaderInSection:` is also implemented.". – rmaddy Oct 02 '14 at 15:16
  • 1
    Fine. What the docs say, they say. Now experiment. The facts are as I have stated. – matt Oct 02 '14 at 15:17
  • And how can you have both `titleForHeaderInSection` and `viewForHeaderInSection`? The table view will only call one of the two (I forget which gets precedence at the moment). – rmaddy Oct 02 '14 at 15:19
  • 1
    Actually there is one more piece of the puzzle, which is that _sometimes_ `viewForHeader` is called _without_ any of those three ways of assigning a height. I have had this happen, where my `viewForHeader` was called and headers showed up just fine, _until one day, with no change on my part, they didn't_. That's when I started experimenting to discover what the minimal requirements are for `viewForHeader` to be called. And now I know. And now so do you. – matt Oct 02 '14 at 15:22
  • If both `titleForHeaderInSection` and `viewForHeaderInSection` are implemented, and assuming `viewForHeaderInSection` returns a UITableViewHeaderFooterView, they will both be called, and in fact this is a perfectly reasonable thing to do. – matt Oct 02 '14 at 15:28
  • Now I tried to implement `titleForHeaderInSection` together with `viewForHeaderInSection` (whereas I only need the view). It works as matt stated but what should `titleForHeaderInSection` return? It only worked for me when returning an empty space. So it's still a compromise because I have to additionaly return things (height or space). – testing Oct 30 '14 at 07:01
  • @matt, do you have any clue for the following http://stackoverflow.com/questions/34665313/getheightforheader-and-getviewforheader-not-called – casillas Jan 07 '16 at 21:58
  • 2
    @texas No, I don't do xamarin. Adding another level of indirection on top of the Cocoa frameworks would just make my head explode. :) – matt Jan 07 '16 at 22:50
  • :-) Just ignore about xamarin, my question is simply that my implementation always calls `TitleHeader` but not calling `ViewHeight` and `ViewHeader`. What might be reason? – casillas Jan 07 '16 at 22:52
  • Any reason you excluded `estimatedSectionHeaderHeight` from your three ways? – mfaani Dec 13 '17 at 21:13
  • @Honey Look at the date on that answer. – matt Dec 14 '17 at 01:59
  • Ouch/hah. Didn't know. You have look at the date I started developing :) but I guess adding that to the answer won't bother. – mfaani Dec 14 '17 at 02:02
24

Giving estimatedSectionHeaderHeight and sectionHeaderHeight values fixed my problem. e.g., self.tableView.estimatedSectionHeaderHeight = 100 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension

Sharukh Mastan
  • 1,491
  • 18
  • 17
  • My issue started after upgrading to Swift 3.1. This solution fixed it. – zevij May 23 '17 at 20:21
  • @pbuchheit Apple Docs says it is available from iOS 7.0+, please have a look here, https://developer.apple.com/documentation/uikit/uitableview/1614957-estimatedsectionheaderheight – Sharukh Mastan Dec 14 '17 at 03:41
  • @Sharukh Mastan Looks like you are correct. For some reason I was getting a warning when I tried using that property, but it went away after doing a clean build. – pbuchheit Dec 14 '17 at 14:08
8

Going off rmaddy 's answer, I was trying to hide the Header view and was returning 0.0f for "tableView:heightForHeaderInSection" and a 0 height View from tableView:viewForHeaderInSection .

After changing from return 1.0f to return 0.0f in tableView:heightForHeaderInSection, the delegate method tableView:viewForHeaderInSection was indeed called.

Turns out my desired effect works without having to use "tableView:heightForHeaderInSection"; but this may be useful to others who are having an issue getting "tableView:heightForHeaderInSection" delegate method called.

rrrrrraul
  • 121
  • 2
  • 7
7

You should implement tableView:heightForHeaderInSection: and set the height for the header >0.

This delegate method goes along with the viewForHeaderInSection: method.

I hope this helps.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
         return 40;
}
Chiara
  • 398
  • 5
  • 10
  • Have you tested that your answer actually works? Becaus eif you read carefully then it's stated that initially the viewForHeaderInSection is called. It's not called only when the table is reloaded! – Karlis Jun 02 '17 at 10:21
6

It's worth briefly noting that if your implementation of tableView:heightForHeaderInSection: returns UITableViewAutomaticDimension, then tableView:viewForHeaderInSection: will not be called.

UITableViewAutomaticDimension assumes that a standard UITableViewHeaderFooterView will be used that is populated with the delegate method tableView:titleForHeaderInSection:.

From comments in the UITableView.h:

Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil.

Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • 1
    if you set ``estimatedSectionHeaderHeight``to some value, ``tableView:viewForHeaderInSection`` will be called (similarly to how auto dimensions for rows works) – GreatWiz Feb 28 '16 at 10:59
  • Interesting, thanks. Back at 7.1 [this subtlety of estimated height was important for cells](http://stackoverflow.com/a/31558458/2547229), so might have been the case for headers too – but it's not terribly relevant now! – Benjohn Feb 29 '16 at 12:22
4

I've just had an issue with headers not showing for iOS 7.1, but working fine with later releases I have tested, explicitly with 8.1 and 8.4.

For the exact same code, 7.1 was not calling any of the section header delegate methods at all, including: tableView:heightForHeaderInSection: and tableView:viewForHeaderInSection:.

After experimentation, I found that removing this line from my viewDidLoad made headers re-appear for 7.1 and did not impact other versions I tested:

// _Removing_ this line _fixed_ headers on 7.1
self.tableView.estimatedSectionHeaderHeight = 80;

… so, there seems to be some kind of conflict there for 7.1, at least.

Benjohn
  • 13,228
  • 9
  • 65
  • 127
4

Same issue occured with me but as I was using automatic height calculation from xCode 9, I cannot give any explicit height value as mentioned above. After some experimentation I got solution, we have to override this method as,

-(CGFloat)tableView:(UITableView *)tableView 
         estimatedHeightForHeaderInSection:(NSInteger)section
{
      return 44.0f;
}

Although I have checked both options

  1. Automatic Height Calculation
  2. Automatic Estimated Height Calculation

from storyboard as apple says, but still I got this weird error.

Please Note: This Error was shown only on IOS-10 version not on IOS-11 version. Maybe it's a bug from xCode. Thanks

Najam
  • 1,129
  • 11
  • 32
1

The reason why viewForHeaderInSection does not get called is for one of two reasons:

Either you did not set up your UITableViewDelegate, or you set up your UITableViewDelegate incorrectly.

Don Brody
  • 1,689
  • 2
  • 18
  • 30
Cem Yilmaz
  • 41
  • 3
0

Here's what I've found (Swift 4) (thanks to this comment on another question)

Whether I used titleForHeaderInSection or viewForHeaderInSection - it wasn't that they weren't getting called when the tableview was scrolled and new cells were being loaded, but any font choices I made for the headerView's textLabel were only appearing on what was initially visible on load, and not as the table was scrolled.

The fix was willDisplayHeaderView:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel?.font = UIFont(name: yourFont, size: 42)
    }
}
RanLearns
  • 4,086
  • 5
  • 44
  • 81
0

In my case I have created header view using UITableviewCell and returning the cell in viewForHeaderInSection like this

return cell

changed this to

return cell.contentView 

Worked for me.

Chris32
  • 4,716
  • 2
  • 18
  • 30
0

In my case

viewForHeaderInSection

was implemented in a derived class far far away that was not bothering to daisy into superclass.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
0

In my case it was cause I did not implement:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
-1

Sometimes setting tableview.delegate or datasource = nil in the viewWillAppear: or viewDidAppear: methods can cause this issue. Make sure not to do this...

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Houzyi
  • 1
-1

I had cut & paste the following two methods from a Swift 2 project into my Swift 3 project which were never called because in Swift 3 these methods must have "-" before the first parameter name.

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44.0
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: B2BTrolleyHeaderFooterView.reuseIdentifier) as! B2BTrolleyHeaderFooterView        
    return headerView
}
koira
  • 1,197
  • 8
  • 10