148

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?

3
  • 2
    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. Commented Apr 9, 2018 at 17:55
  • As @Honey says 0.0 does not work. I have a more thorough answer for the same issue here: stackoverflow.com/a/22185534/2789144 Commented Oct 15, 2018 at 20:33
  • Thanks @mfaani. 0.0001 is the solution for grouped tableView still in 2024! Commented Aug 1, 2024 at 11:02

11 Answers 11

268

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.

Sign up to request clarification or add additional context in comments.

9 Comments

You should return 5.0f instead of 5.0, because of double - float conflicts
You can even return 0.00001f as a height and you'll get a height of 0 pixels/points.
As @Klass points out, you cannot use 0 as the height – you will get the default height.
Why not use CGFLOAT_MIN? It's made for these kind of scenarios :)
If using swift, you can return CGFloat.leastNonzeroMagnitude
|
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)

3 Comments

return 0.0001; is working for me. I am using xcode 4.6 and iOS 6.1
I only need to return CGFLOAT_MIN in the first two methods and it works fine.
instead of 0 use return .leastNormalMagnitude
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.

4 Comments

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.
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.
With Xcode 6 and iOS 8 it seems to be working with just the config in the storyboard.
they're set to 18 on xcode 7 by default. but that's by far the easiest solution!
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; 

Comments

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.

Comments

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 } 

Comments

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.

Comments

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)

Comments

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.

Comments

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 } 

Comments

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 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.