128

I need to reduce the space between two sections ofUITableView. I looked at this question but the solution doesn't allow for my custom header view because it combines the space of the footer and header.

Here is a picture of the UITableView. The black color is the UITableView background color.

tableview screenshot

Community
  • 1
  • 1
Joel
  • 1,585
  • 2
  • 10
  • 20

19 Answers19

231

On iOS 15 you may want to reduce the sectionHeaderTopPadding

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}
rockdaswift
  • 9,613
  • 5
  • 40
  • 46
169

Did you try override this function:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Icaro
  • 14,585
  • 6
  • 60
  • 75
43

I think you can solve this by adjusting the footer view height to its min: in Storyboard or XIB.enter image description here

I don't know what you have written in your code for footer height. Sorry if I am wrong.

Possible duplicate of Hide footer view in UITableView

Community
  • 1
  • 1
Sudhin Davis
  • 2,010
  • 13
  • 18
  • When I try that the minimum footer size is 1 which still leaves a 1point gap – Joel May 23 '15 at 16:36
  • Either you can change the color of that footer to the header section color.. So that the 1px black color can be ignored. – Sudhin Davis May 25 '15 at 03:39
  • @SudhinDavis you are right that there is footer height in xib but if we want no footer then Icaro's answer http://stackoverflow.com/a/30386480/2849443 is correct – Pooja Shah Nov 09 '15 at 06:08
  • That answer is far better than mine. Since that will also remove the extra lines from empty cells. Thanks. – Sudhin Davis Nov 09 '15 at 07:09
  • I liked possible solutions given in http://stackoverflow.com/questions/11445301/hide-footerview-in-iphone-tableview – Alphonse R. Dsouza Nov 11 '15 at 09:53
  • If a height of 1 for the footer is still too large you can return 0.01 in `heightForFooterInSection` in code. – Gordonium Mar 17 '16 at 13:48
29

For Swift 4+ you need to implement these two methods

extension MyViewController : UITableViewDelegate {

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

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

}
Ladd.c
  • 973
  • 11
  • 11
  • Those are UITableViewDelegate methods. You do not override them. – Drew C Dec 03 '19 at 18:29
  • Ok I edited it my answer to a better and general implementation. Cheers. – Ladd.c Dec 12 '19 at 22:31
  • 2
    Nice! Not sure why this has not more upvotes. It's important to return a `UIView` or `nil` in `viewForFooterInSection`. Just using `heightForFooterInSection` (like mentioned in other answers does not work) – laka Feb 18 '22 at 23:20
13

For Swift 3

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}
haris
  • 3,775
  • 1
  • 25
  • 28
10

For Swift 5+:

There is some space for the headers and footers by default. That's why I was having the problem of setting an exact separation for the sections.

My solution to having a separation between 2 sections is the following:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return 24
    } else if section == 1 {
        return 32
    } else {
        return 40
    }
}
    
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    nil
}
    
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    nil
}
    
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    CGFloat.leastNormalMagnitude
}

As you see for viewForFooterInSection and viewForHeaderInSection I needed to return nil.

In case you only want to change the footerHeight, just return CGFloat.leastNormalMagnitude for heightForHeaderInSection, and return the heights for each section in heightForFooterInSection.

b m gevariya
  • 300
  • 1
  • 4
  • 12
Fernando Cardenas
  • 1,203
  • 15
  • 19
  • Adding viewForFooterInSection table view delegate function alongside heightForFooterInSection fixed the spacing for me – Jacob G Mar 07 '23 at 17:51
7

Along with the answer posted by Icaro I would like to add that you also need to implement the tableView:viewForFooterInSection: method returning nil for the section you want to remove the empty space below It will then become:

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
Nicholas Allio
  • 717
  • 2
  • 9
  • 28
6

You need to use the method heightForHeaderInSection for defining the space between header & cell text. You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use CGFLOAT_MIN which is 0.000001f. Giving you an example, how you can use different section with different header heights:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 2)
    {
        return 55.0;
    }
    else
    {
        return CGFLOAT_MIN;
    }
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Ash
  • 5,525
  • 1
  • 40
  • 34
  • 2
    This is wrong. heightForHeaderInSection defines the height of the headerView for the section, not the space between them, which is what the OP was asking about. https://developer.apple.com/reference/uikit/uitableviewdelegate/1614855-tableview?language=objc – davidrynn Mar 09 '17 at 19:23
6

I just simply had to reduce the top padding for the tableview section header:

tableView.sectionHeaderTopPadding = 0
Euan Traynor
  • 420
  • 2
  • 11
3

TableView Delegate methods doesn't effect with float value is 0.0f. Try giving a value greater than that.

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001f;
}

- (UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}
Mithra Singam
  • 1,905
  • 20
  • 26
3

In Xcode 13.2, you can adjust the height of the header and footer of sections in the storyboard - see screenshot below:

Height of Header and Footer of Sections in Xcode 13.2

rodorgas
  • 962
  • 2
  • 12
  • 29
maxMas
  • 49
  • 7
2

This also may help :

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
polarware
  • 2,499
  • 1
  • 24
  • 18
2

Select the tableView in your storyboard/objectCode and ensure that the style is set to Plain, instead of Grouped. You can find this setting in the attributes "Inspector" tab.

let myTableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.register(TableCellClass.self, forCellReuseIdentifier: "cellId")
        tableView.backgroundColor = UIColor(red: 123/255, green: 190/255, blue: 120/255, alpha: 1)
        tableView.separatorStyle = .none
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()
Nah
  • 1,690
  • 2
  • 26
  • 46
gsk_fs
  • 94
  • 1
  • 10
  • please chek and verify in your code that **Style is set to plain** if story board you can find this word in tableView (Attribute inspector) – gsk_fs Dec 01 '20 at 09:33
2

Rather than implementing the UITableViewDelegate methods and defining the sectionFooterHeight via CGFloat.leastNormalMagnitude, one can alternatively just

tableView.sectionFooterHeight = 0

and the spacing between sections while no footer is present will go away.

The mechanism is that by default this value is set to UITableView.automaticDimension.

As long as

  • it stays UITableView.automaticDimension
  • there are no delegate/dataSource methods that implement the configuration of footer i.e. titleForFooterInSection/viewForFooterInSection
  • table view's style is set to .grouped

then UITableView will deliberately insert a spacing between sections with no view.

You change sectionFooterHeight to 0, the magic goes away.

Isaaс Weisberg
  • 2,296
  • 2
  • 12
  • 28
0

You can do it by implement the delegate heightForHeaderInSection & heightForFooterInSection.

The return vaule should not be 0, even if the SectionHeader or the height of SectionFooter is 0, it need a very small value, try CGFLOAT_MIN.

for my example:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
    return 46;
}
return CGFLOAT_MIN;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
} 
MichaelMao
  • 528
  • 6
  • 15
0

Work for me

tableView.sectionFooterHeight = 10
// ...

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

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Mirek
  • 1
  • 1
0

swift 5 iOS 15

self.tableView.estimatedSectionFooterHeight = 16.0 // spacing between Sections
0

If you need spacing only for the first cell at the top of your UITableView and your header sections after scrolling must have no space, instead of using contentInsets.top do this:

Add fake UIView with your spacing by adjusting it's height and place it into UITableView.tableHeaderView

Here's how it should look like (using SnapKit):

let spaceView = UIView()
spaceView.snp.makeConstraints {
    $0.height.equalTo(10)
}
    
tableView?.tableHeaderView = spaceView

tableHeaderView is not fixed, so in this case, spacing at the top of tableView will be 10, but after scrolling tableView spacing from top will be gone, spacing only stays at the top.

-1

This will work iOS 15 and greater

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}

It will reduce automatic space between header and cell, if you have multiple header sections.

eglease
  • 2,445
  • 11
  • 18
  • 28