126

I want to set the height of the first header in my UITableView. For the other headers I want them to remain the default height. What value/constant can I put in place of "someDefaultHeight" in the code below?

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return kFirstHeaderHeight;

    return someDefaultHeight;
}

Thanks

rein
  • 32,967
  • 23
  • 82
  • 106
  • why dont you try different values till you getone that you are happy with? – Daniel Jul 29 '09 at 14:50
  • 4
    @Daniel - if Apple ever decides to change the default row height value then I need to ensure that my app does not hard code this value (to some arbitrary amount). It's best to pull this information out of a constant if it is declared somewhere. – rein Jul 29 '09 at 23:33

8 Answers8

205

In IOS 5.0 onwards you can return UITableViewAutomaticDimension in most of the delegate methods. Its at the bottom of the documentation page

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == CUSTOM_SECTION)
    {
        return CUSTOM_VALUE;
    }
    return UITableViewAutomaticDimension;
}
Ajaxharg
  • 2,974
  • 2
  • 18
  • 19
  • 1
    hmm.. As for me `UITableViewAutomaticDimension` returns `-1` (hardcoded const) and I don't see any sections at all in my `UITableView`. – skywinder Sep 22 '13 at 21:17
  • why `UITableViewAutomaticDimension` shows -1 when `NSLog` it? – S1U Sep 24 '13 at 23:49
  • 31
    This only works when you area using: `- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section` if you're implementing the `- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section` method, this does not work. – SuperSaiyen Sep 26 '13 at 18:43
  • 2
    It's worth noting, if you implement this and the estimate delegate method and return `UITableViewAutomaticDimension` it will have a zero height. – Sam Soffes May 12 '15 at 00:09
  • 4
    @SuperSaiyen - It does work with `viewForHeaderInSection` you just need to set `estimatedSectionHeaderHeight` – Robert Oct 07 '15 at 15:19
  • In my case, it worked after i set self.tableView.estimatedSectionHeaderHeight = MY_VALUE; in viewDidLoad – guilherme.minglini Dec 28 '16 at 14:09
51

From checking the defaults in my app it looks like for a grouped table the default is a height of 22 and for a non-grouped table the default is a height of 10.

If you check the value of the property sectionHeaderHeight on your tableview that should tell you.

Kimpoy
  • 1,974
  • 3
  • 21
  • 38
paulthenerd
  • 9,487
  • 2
  • 35
  • 29
26

Actually do the trick :)

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return kFirstSectionHeaderHeight;
    return [self sectionHeaderHeight];
}
BadPirate
  • 25,802
  • 10
  • 92
  • 123
  • 2
    I think you meant `return [self.tableView sectionHeaderHeight];`, or better yet, `return [tableView sectionHeaderHeight];`. However, both return -1 for me, perhaps because I'm not using a nib or storyboard. – jk7 Jan 30 '16 at 23:37
7

For the sake of completeness: in iOS7+ the height for grouped style section headers is 55.5 for the first and 38 for following headers. (measured with DCIntrospect)

ronhippler
  • 106
  • 1
  • 6
5

For swift 4.2 you should return UITableView.automaticDimension

Muvimotv
  • 853
  • 9
  • 14
2

To get the default height, just let super handle it:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return kFirstHeaderHeight;

    return [super tableView:tableView heightForHeaderInSection:section];
}
Hendrik
  • 491
  • 4
  • 7
  • `[super tableView:tableView heightForHeaderInSection:section];` returns 0 for me, perhaps because I'm not using a nib or storyboard. – jk7 Jan 30 '16 at 23:39
  • 1
    This only works if you are subclassing UITableViewController. – Wallace Oct 06 '16 at 22:28
2

I'm not sure what the correct answer is here, but neither 10 or 22 appears to be the correct height for a grouped table view in iOS 5. I'm using 44, based on this question, and it at least appears to roughly the correct height.

Community
  • 1
  • 1
Jason George
  • 6,992
  • 8
  • 44
  • 60
-1

This should do the trick

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == CUSTOM_SECTION)
    {
        return CUSTOM_VALUE;
    }
    return [tableView rowHeight];
}
Eimantas
  • 48,927
  • 17
  • 132
  • 168