146

Is there a way to reduce the space between two sections of a UITableView? There are about 15 pixels between every single section I have. I did already try to return 0 for -tableView:heightForFooterInSection: and -tableView:heightForHeaderInSection: but that doesn't change anything.

Any suggestions?

Brian
  • 14,610
  • 7
  • 35
  • 43
flohei
  • 5,248
  • 10
  • 36
  • 61
  • 1
    I was using a **grouped** tableView and setting the header/footer height to `0.0`. But it was showing a gray area with a (default) 30point height. Using `0.0` is unaccepted. you must use any value above `0.0` e.g. `0.0001`. – mfaani Apr 09 '18 at 17:55
  • As @Honey says `0.0` does not work. I have a more thorough answer for the same issue here: https://stackoverflow.com/a/22185534/2789144 – James Nelson Oct 15 '18 at 20:33

11 Answers11

269

It was a bit tricky, but try this code:

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

    return 1.0;
}

- (CGFloat)tableView:(UITableView*)tableView 
           heightForFooterInSection:(NSInteger)section {
    return 5.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];
}

Change the values accordingly. To remove the space, I think 0.0 will not be accepted. The smallest sane value seems to be 1.0.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Tomen
  • 4,854
  • 5
  • 28
  • 39
  • 2
    You should return 5.0f instead of 5.0, because of double - float conflicts – LeonS Oct 05 '11 at 11:36
  • 45
    You can even return 0.00001f as a height and you'll get a height of 0 pixels/points. – Klaas Jan 06 '13 at 22:28
  • 1
    As @Klass points out, you cannot use `0` as the height – you will get the default height. – zekel Jan 14 '14 at 21:46
  • I've heard that fractions introduce some blurring around the text, so it might be safer to go with a whole integers as Tomen suggests. – Chris Nolet Oct 17 '14 at 01:46
  • 1
    you must NOT use values like "0.00001", it will break rendering of bottom views. all your texts will be blurred badly. use only integer values – storoj Oct 23 '14 at 10:01
  • 27
    Why not use CGFLOAT_MIN? It's made for these kind of scenarios :) – Andrei Filip May 11 '15 at 09:38
  • @AndreiFilip: Have you tried it out? Does it work? Clearly the value 0 is made for the kind of scenario where you want a height of 0, and _that_ doesn't work. – gnasher729 Dec 22 '15 at 10:54
  • I can confirm that CGFLOAT_MIN does indeed work. Make sure you also return a basic UIView as the header and/or footer views. – Christian Gossain Feb 16 '16 at 17:14
  • 12
    If using swift, you can return `CGFloat.leastNonzeroMagnitude` – oztune Oct 24 '16 at 19:01
142

For all who want to shrink the distance to 0 you have to use:

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

Because the serving of the UITableViewDelegate does only make an effect starting from floats greater than zero.

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

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

(using iOS 4.1 with XCode 4.0.2)

Martin Stolz
  • 5,176
  • 3
  • 21
  • 19
33

You can actually set the footer/header/cell heights in Interface Builder under the size tab. By default the header/footer are set at 10.0.

Chris
  • 331
  • 3
  • 2
  • I think such behavior was added in iOS 5.0 or iOS 6.0, but yes - it's now much easier to setup distance between groups. – Vlas Voloshin Jun 05 '13 at 07:22
  • 2
    I know this is old but I wanted to comment that as of XCode 5 and iOS 7, it seems like this HAS to be done in code. I set it in interface builder to 0 and it still was set to 1 until I set it in code to 0. – Ahmad Jan 03 '14 at 12:06
  • With Xcode 6 and iOS 8 it seems to be working with just the config in the storyboard. – Murray Sagal Nov 20 '14 at 01:45
  • they're set to 18 on xcode 7 by default. but that's by far the easiest solution! – static0886 Apr 15 '16 at 14:04
29

You have to reduce the section header/footer height. Then the space between sections will be reduce.

try this code

It works for me :-)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;
Sotiris
  • 38,986
  • 11
  • 53
  • 85
Jirune
  • 2,310
  • 3
  • 21
  • 19
23

You can also reduce the height of section footer and header from the storyboard. In the tableview -> size inspector. Go to Section Height.

Size inspector for UITableView in storyboard.

By default it is set to 22 for Plain style table and 10 for grouped style table. You can configure values by increasing / decreasing the values for header and footer separately.

iosCurator
  • 4,356
  • 2
  • 21
  • 25
7

Use this perhaps, 0 will not work as expected

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

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}
Stan
  • 1,513
  • 20
  • 27
6

For me the issue was because I was using a grouped table view style (UITableViewStyleGrouped). When I changed it to a plain style (UITableViewStylePlain) my tableView:heightForHeaderInSection: method was the only thing determining the size of each section header.

Liron Yahdav
  • 10,152
  • 8
  • 68
  • 104
3

UPDATE FOR iOS7: Due to ARC autorelease update, here's @Martin Stolz code edited.

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return 6;
    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)];
}

(Using iOS7, Xcode v5.0)

Chisx
  • 1,976
  • 4
  • 25
  • 53
0

For me just this issue got resolved just by using the following code,

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 0 ? 1 : 20 // 20 is my other sections height
}

Returning 1 for the first section has solved the issue.

Bharath
  • 2,064
  • 1
  • 14
  • 39
0

To change header/footer space the following methods have to be implemented:

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

AND

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

(use corresponding methods to change footer height)

The following code completely removes spaces around sections:

public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return nil
}

public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return nil
}

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

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return .leastNonzeroMagnitude
}
Murlakatam
  • 2,729
  • 2
  • 26
  • 20
0

Swift 5, iOS 13+

Option 1 - When creating the table view

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

Option 2 - More flexible for other views and sections

// Top space for sections
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0
}

// Bottom space for sections
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0
}
Arturo
  • 3,254
  • 2
  • 22
  • 61