Space Between Sections in Uitableview

Space Between Sections in UITableview

Did you try override this function:

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

Reducing the space between sections of the UITableView

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.

How to add space between UITableView sections?

On iOS 15 you can add padding to the section header

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

Extra Space Between UITableViewSections in iOS 15

In iOS 15 the property sectionHeaderTopPadding was added. It affects that exact space. The property's default value is automaticDimension. Setting it to 0.0 fixes the problem.

Since the property is only available in iOS 15, you may want to wrap it with an availability block:

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

Here's the original code snippet from the question including necessary changes:

import UIKit

final class ViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

tableView.separatorColor = .yellow
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
}
}

// The rest is without changes.

}

Here's the output in iOS 15 after the change:

iOS 15 after update

How to change the height BETWEEN sections in GROUPED UITableView?

You could use footer views between each of your sections and give them the size of the space you desire by implementing the following methods in your UITableView's delegate.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

In the first delegate method you can return a simple UIView instance, and in the second you can return the height that this view should be. This second delegate method should allow you to control the gap between sections.

iPhone UITableView : How to remove the spacing between sections in group style table?

Try this..

   self.tableView.rowHeight = 0; // in viewdidload
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // in viewdidload

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

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return <your header height>;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return <your header view>;
}

Also have table seprator as none.

Hide a section / space between sections in UITableView

I’ve found a solution in this thread.

Long story short: You have to set self.tableView.sectionHeaderHeight = 0.0 as well as self.tableView.sectionFooterHeight = 0.0 i.e. in viewDidLoad.

For whatever reason the heightForHeader and heightForFooter delegate methods are ignored.

Remove vertical space between sections in a Grouped UITableView

For some reason tableView.estimatedSectionFooterHeight = 0.0 was not working but tableView.sectionFooterHeight = 0.0 did, even with the UITableView.style = .grouped.

Also I was using an empty UIView as my footer view.

tableView.tableFooterView = UIView(frame: CGRect.zero)
tableView.sectionFooterHeight = 0.0

Thanks for all your help!

How to create the spacing between tableview sections? ( Header and Footer of every section are already custom views)

From what I can tell there is no "easy" way of doing what you want. But it's not that hard either. As @Tom suggested you need to use an empty cell for your empty spaces. But the empty cell has to be put in it's own section that has no header or footer.

I put together something really quick to to get you started (add this to a Playground to see it in action). In the image bellow headers are green, content cells are blue, footers yellow and the spacers white:

Sample Image

import UIKit
import PlaygroundSupport

class ContentCell: UITableViewCell {

}

class SectionSpacerCell: UITableViewCell {

}

class MyViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableView: UITableView!

private var sections: [Int] = []

override func loadView() {
let view = UIView()
view.backgroundColor = .white

tableView = UITableView(frame: view.bounds)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

tableView.register(ContentCell.self, forCellReuseIdentifier: "ContentCell")
tableView.register(SectionSpacerCell.self, forCellReuseIdentifier: "SectionSpacerCell")

tableView.dataSource = self
tableView.delegate = self

view.addSubview(tableView)
self.view = view
}

override func viewDidLoad() {
super.viewDidLoad()

sections = [4, 7, 4, 2, 7]
tableView.reloadData()
}

// MARK: UITableViewDataSource

func numberOfSections(in tableView: UITableView) -> Int {
return sections.count > 0 ? sections.count + (sections.count - 1) : 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section % 2 == 0 ? sections[section / 2] : 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section % 2 == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as? ContentCell else {
fatalError("Oops!")
}
cell.backgroundColor = .blue
return cell
}
else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SectionSpacerCell") as? SectionSpacerCell else {
fatalError("Oops!")
}
cell.backgroundColor = .white
return cell
}
}

// MARK: - UITableViewDelegate

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section % 2 == 0 {
return 40
}
return 0
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section % 2 == 0 {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
headerView.backgroundColor = .green
return headerView
}
return nil
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if section % 2 == 0 {
return 40
}
return 0
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section % 2 == 0 {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
headerView.backgroundColor = .yellow
return headerView
}
return nil
}

}

PlaygroundPage.current.liveView = MyViewController()

How to add space between UITableView sections?

On iOS 15 you can add padding to the section header

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


Related Topics



Leave a reply



Submit