flohei
flohei

Reputation: 5308

Reducing the space between sections of the UITableView

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?

Upvotes: 148

Views: 101211

Answers (11)

Arturo
Arturo

Reputation: 4180

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
}

Upvotes: 0

Murlakatam
Murlakatam

Reputation: 3265

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
}

Upvotes: 0

Stan
Stan

Reputation: 1583

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
}

Upvotes: 7

Bharath
Bharath

Reputation: 2114

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.

Upvotes: 0

Liron Yahdav
Liron Yahdav

Reputation: 10732

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.

Upvotes: 6

iosCurator
iosCurator

Reputation: 4466

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.

Upvotes: 23

Tomen
Tomen

Reputation: 4854

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.

Upvotes: 268

Chisx
Chisx

Reputation: 1986

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)

Upvotes: 3

Martin Stolz
Martin Stolz

Reputation: 5296

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)

Upvotes: 142

Jirune
Jirune

Reputation: 2340

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;

Upvotes: 29

Chris
Chris

Reputation: 331

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.

Upvotes: 33

Related Questions