Reducing the space between sections of the UITableView

IphoneIosUitableview

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

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)

Solution 3 - Iphone

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.

Solution 4 - Iphone

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;

Solution 5 - Iphone

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.

Solution 6 - Iphone

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.

Solution 7 - Iphone

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
}

Solution 8 - Iphone

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)

Solution 9 - Iphone

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.

Solution 10 - Iphone

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfloheiView Question on Stackoverflow
Solution 1 - IphoneTomenView Answer on Stackoverflow
Solution 2 - IphoneMartin StolzView Answer on Stackoverflow
Solution 3 - IphoneChrisView Answer on Stackoverflow
Solution 4 - IphoneJiruneView Answer on Stackoverflow
Solution 5 - IphoneiosCuratorView Answer on Stackoverflow
Solution 6 - IphoneLiron YahdavView Answer on Stackoverflow
Solution 7 - IphonestanView Answer on Stackoverflow
Solution 8 - IphoneChisxView Answer on Stackoverflow
Solution 9 - IphoneBharathView Answer on Stackoverflow
Solution 10 - IphoneMurlakatamView Answer on Stackoverflow