RoundOutTooSoon
RoundOutTooSoon

Reputation: 9891

Eliminate extra separators below UITableView

When I set up a table view with 4 rows, there are still extra separators lines (or extra blank cells) below the filled rows.

How would I remove these cells?

image for extra separator lines in UITableView

Upvotes: 771

Views: 200063

Answers (30)

Shakeel Ahmed
Shakeel Ahmed

Reputation: 6021

Swift 3 /Swift 4 /Swift 5 +, Very Easy and simple way

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
      //MARK:- For Hiding the extra lines in table view.
    tableView?.tableFooterView = UIView()
}

OR

override func viewDidLoad(_ animated: Bool) {
    super.viewDidLoad(animated)
      //MARK:- For Hiding the extra lines in table view.
    tableView?.tableFooterView = UIView()
}

Upvotes: 1

Pratik
Pratik

Reputation: 2399

You can remove separator of empty rows by just adding minor height of footer

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

Upvotes: 0

Babatunde Adeyemi
Babatunde Adeyemi

Reputation: 14448

If you are using Swift, add the following code to viewDidLoad of the controller that manages the tableview:

override func viewDidLoad() {
    super.viewDidLoad()

    //...

    // Remove extra separators
    tableView.tableFooterView = UIView()
}

Upvotes: 15

Atanu Mondal
Atanu Mondal

Reputation: 1724

Try with this

for Objective C

- (void)viewDidLoad 
 {
  [super viewDidLoad];
  // This will remove extra separators from tableview
  self.yourTableView.tableFooterView = [UIView new];
}

for Swift

override func viewDidLoad() {
 super.viewDidLoad()
 self.yourTableView.tableFooterView = UIView()
}

Upvotes: 2

Kalpesh Panchasara
Kalpesh Panchasara

Reputation: 1750

To eliminate extra separator lines from bottom of UItableview programmatically, just write down following two lines of code and it will remove extra separator from it.

tableView.sectionFooterHeight = 0.f;
tableView.sectionHeaderHeight = 0.f;

This trick working for me all the time, try yourself.

Upvotes: 3

Marcelo Gracietti
Marcelo Gracietti

Reputation: 3131

Swift works great with:

tableView.tableFooterView = UIView()

Upvotes: 7

o.shnn
o.shnn

Reputation: 1123

Advancing J. Costa's solution: You can make a global change to the table by putting this line of code:

[[UITableView appearance] setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

inside the first possible method (usually in AppDelegate, in: application:didFinishLaunchingWithOptions: method).

Upvotes: 7

hoptown
hoptown

Reputation: 125

In Swift (I'm using 4.0), you can accomplish this by creating a custom UITableViewCell class, and overriding the setSelected method. Then the separator insets all to 0. (my main class with the table view has a clear background) color.

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // eliminate extra separators below UITableView
    self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

Upvotes: 0

user3900346
user3900346

Reputation: 199

In case you have a searchbar in your view (to limit the number of results for example), you have to also add the following in shouldReloadTableForSearchString and shouldReloadTableForSearchScope:

controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];

Upvotes: 0

Stanislau Baranouski
Stanislau Baranouski

Reputation: 1451

You may find lots of answers to this question. Most of them around manipulation with UITableView's tableFooterView attribute and this is proper way to hide empty rows. For the conveniency I've created simple extension which allows to turn on/off empty rows from Interface Builder. You can check it out from this gist file. I hope it could save a little of your time.

extension UITableView {

  @IBInspectable
  var isEmptyRowsHidden: Bool {
        get {
          return tableFooterView != nil
        }
        set {
          if newValue {
              tableFooterView = UIView(frame: .zero)
          } else {
              tableFooterView = nil
          }
       }
    }
}

Usage:

tableView.isEmptyRowsHidden = true

enter image description here

Upvotes: 6

Suresh Varma
Suresh Varma

Reputation: 9740

I have added this small tableview extension that helps throughout

extension UITableView {
     func removeExtraCells() {
         tableFooterView = UIView(frame: .zero)
     }
}

Upvotes: 1

Casebash
Casebash

Reputation: 118962

I was using a table view to show a fixed number of fixed height rows, so I simply resized it and made it non-scrollable.

Upvotes: 3

dineshthamburu
dineshthamburu

Reputation: 1151

Removing extra separator lines for empty rows in UITableView in Swift

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.yourTableview.tableFooterView = UIView()
}

Upvotes: 76

Alok C
Alok C

Reputation: 2857

Quick and easy Swift 4 way.

override func viewDidLoad() {
     tableView.tableFooterView = UIView(frame: .zero)
}

If you are having static cells. You can also turn off the separator from Inspector window. (this won't be desirable if you need the separator. In that case use method shown above) inspector window

Upvotes: 2

Muzahid
Muzahid

Reputation: 5186

UIKit does not create empty cell when the tableView has a tableFooterView. So we can make a trick and assign a zero height UIView object as footer of the tableView.

tableView.tableFooterView = UIView()

Upvotes: 0

Nike Kov
Nike Kov

Reputation: 13744

Swift 4.0 Extension

Just a little extension for the storyboard:

enter image description here

extension UITableView {
    @IBInspectable
    var hideSeparatorForEmptyCells: Bool {
        set {
            tableFooterView = newValue ? UIView() : nil
        }
        get {
            return tableFooterView == nil
        }
    }
}

Upvotes: 2

Gefilte Fish
Gefilte Fish

Reputation: 1748

If you have only one section, then the quickest and easiest way is to just set the Table View Style from "Plain" to "Grouped". (see image)

TableView Grouped

If you have more sections, you might need to set the header height to zero (depending on your/your customer's/your project manager's taste)

If you have more sections, and don't want to mess with the headers (even if it is just one line in the simplest case), then you need to set a UIView as a footer, as it was explained in the previous answers)

Upvotes: 2

wkw
wkw

Reputation: 3863

Here's another way to do that w/out the grouped table style, and one you'd probably not guess. Adding a header and footer to the table (perhaps one or the other suffices, haven't checked) causes the separators to disappear from the filler/blank rows.

I stumbled onto this because I wanted a little space at the top and bottom of tables to decrease the risk of hitting buttons instead of a table cell with meaty fingers. Here's a method to stick a blank view in as header and footer. Use whatever height you like, you still eliminate the extra separator lines.

- (void) addHeaderAndFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.myTableView setTableHeaderView:v];
    [self.myTableView setTableFooterView:v];
    [v release];
}

In response to @Casebash, I went back to the code in my app ("AcmeLists" List Manager in iTunes store...) and short-circuited the addHeaderAndFooter method to verify. Without it, I have the extra row separators; with the code, I have what you see in this window snap: no table row separators picture. So I'm not sure why it wouldn't have worked for you. Moreover, it makes sense to me that having any custom footer on a table view would necessarily have to stop drawing row separators for blank rows below it. That would be hideous. For reference, I looked at tables where there were more rows than could be viewed on screen, and then for a table with two rows. In both cases, no extraneous separators.

Perhaps your custom views were not actually added. To check that, set the background color to something other than clearColor, e.g., [UIColor redColor]. If you don't see some red bars at the bottom of the table, your footer wasn't set.

Upvotes: 130

J. Costa
J. Costa

Reputation: 7670

Interface builder (iOS 9+)

Just drag a UIView to the table. In storyboard, it will sit at the top below your custom cells. You may prefer to name it "footer".

Here it is shown in green for clarity, you'd probably want clear color.

Note that by adjusting the height, you can affect how the "bottom bounce" of the table is handled, as you prefer. (Height zero is usually fine).

enter image description here


To do it programmatically:

Swift

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.tableFooterView = UIView()
}

Objective-C

iOS 6.1+

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [UIView new];
}

or if you prefer,

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

Historically in iOS:

Add to the table view controller...

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     // This will create a "invisible" footer
     return CGFLOAT_MIN;
 }

and if necessary...

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{        
    return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}

Upvotes: 1566

Sudipto Roy
Sudipto Roy

Reputation: 6795

just add this code (Swift) . .

tableView.tableFooterView = UIView()

Upvotes: 7

Hari c
Hari c

Reputation: 1159

If you want to remove unwanted space in UITableview you can use below two methods

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

Upvotes: 1

Darko
Darko

Reputation: 9855

You can just add an empty footer at the end then it will hide the empty cells but it will also look quite ugly:

tableView.tableFooterView = UIView()

enter image description here

There is a better approach: add a 1 point line at the end of the table view as the footer and the empty cells will also not been shown anymore.

let footerView = UIView()
footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1)
footerView.backgroundColor = tableView.separatorColor
tableView.tableFooterView = footerView

enter image description here

Upvotes: 8

Cœur
Cœur

Reputation: 38727

If you don't want any separator after the last cell, then you need a close to zero but non-zero height for your footer.

In your UITableViewDelegate:

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

Upvotes: 5

TPG
TPG

Reputation: 3219

I just add this line at the ViewDidLoad function and problem fixed.

tableView.tableFooterView = [[UIView alloc] init]; 

Upvotes: -1

Parth Changela
Parth Changela

Reputation: 955

uitableview extra separator line hide extra separators lines hide in swift 3.0

 self.tbltableView.tableFooterView = UIView(frame: .zero)

Upvotes: 5

BAP
BAP

Reputation: 1402

I had some luck implementing a single piece of the accepted answer (iOS 9+, Swift 2.2). I had tried implementing:

self.tableView.tableFooterView = UIView(frame: .zero)

However, there was no effect on my tableView - I believe it may have something to do with the fact that I was using UITableViewController.

Instead, I only had to override the viewForFooterInSection method (I did not set the tableFooterView elsewhere):

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView(frame: .zero)
}

This worked fine for a tableView with a single section (if you have multiple sections, you need to specify the last one).

Upvotes: 2

Todd Vanderlin
Todd Vanderlin

Reputation: 9

if you are sub-classing the UITableView you need to do this...

-(void)didMoveToSuperview {
    [super didMoveToSuperview];
    self.tableFooterView = [UIView new];
}

Upvotes: -1

Sebastian
Sebastian

Reputation: 9051

For Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
    }

Upvotes: 14

Guntis Treulands
Guntis Treulands

Reputation: 4762

I would like to extend wkw answer:

Simply adding only footer with height 0 will do the trick. (tested on sdk 4.2, 4.4.1)

- (void) addFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];

    [self.myTableView setTableFooterView:v];
}

or even simpler - where you set up your tableview, add this line:

//change height value if extra space is needed at the bottom.
[_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];

or even simplier - to simply remove any separators:

[_tableView setTableFooterView:[UIView new]];

Thanks to wkw again :)

Upvotes: 35

vishnu
vishnu

Reputation: 715

Try this

self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];

Upvotes: 0

Related Questions