85

I have a UITableView and I have only 3 rows in it, and I can see those 3 rows. The problem is the cells that are empty: I can see lines there. I don't want to see those lines.

Any idea how to remove those lines?

Below is image for what I am looking for.

Image Link

jscs
  • 63,694
  • 13
  • 151
  • 195
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 3
    http://stackoverflow.com/questions/1369831/eliminate-extra-separators-below-uitableview-in-iphone-sdk – shahid-rasheed Jan 22 '13 at 14:27
  • 2
    to create a screen shot, press command+shift+4, then the mouse cursor will be changed to a plus symbol, click and drag the required area,when U release the mouse button, the image will be captured and saved in the desktop. – chandru Dec 20 '13 at 09:41
  • 4
    I love how the background for the screenshot is the SO page with similar questions, as if to say "Oh lords of SO, please forgive me. I did search, I really really did" :D – user1349663 Mar 26 '14 at 05:45

16 Answers16

147

Even simpler than Andrey Z's reply: Simply make a bogus tableFooterView in your UITableView class:

self.tableFooterView = [UIView new]; // to hide empty cells

and Swift:

tableFooterView = UIView()

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
89

You can hide UITableView's standard separator line by using any one of the below snippets of code. The easiest way to add a custom separator is to add a simple UIView of 1px height:

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // set color as you want.
[cell.contentView addSubview:separatorLineView];

OR

    self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
    self.tblView.delegate=self;
    self.tblView.dataSource=self;
    [self.view addSubview:self.tblView];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.tblView setTableHeaderView:v];
    [self.tblView setTableFooterView:v];
    [v release];

OR

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [[UIView new] autorelease];
}

OR

And also check nickfalk's answer, it is very short and helpful too. And you should also try this single line,

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

Not sure but it's working in all the version of iOS that I checked, with iOS 5 and later, up to iOS 7.

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • 7
    Just a note, using the second option will remove the separator line for the last cell, which may not be what you want. The first option will retain the separator line, though I recommend just using the following code: `[self.tableView setTableFooterView:[UIView new]];` for an invisible footer as described in an answer below. – runmad Nov 14 '13 at 17:00
  • @iPatel I am using the third option, when I focus the last table cell, the seperator appears...any fix for this? – DarkLeafyGreen Mar 31 '14 at 20:02
  • Would someone mind giving a little bit of context for the first option? I'm not sure where that's supposed to go. – Pepedou Apr 05 '16 at 17:53
  • @Pepedou your modifying a cell, so if you want it to be applied to a single instance then you'd do in the `cellForAtIndex` or if you want it applied to a *tableViewCell` class then you would do it in it's class so all it's instances would be affected... – mfaani May 12 '17 at 16:20
8

Updated answer for swift & iOS 9. This works for tableviews of the .Plain variety.

 tableView.tableFooterView = UIView()
seo
  • 1,959
  • 24
  • 18
  • This solution was already provided over two years previously: http://stackoverflow.com/a/14461000/603977 – jscs Jun 09 '16 at 18:11
7

Transparent UIView as a tableView footer with 1px height will do the trick.

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];
Andrey Zverev
  • 4,409
  • 1
  • 28
  • 34
  • 1
    OR : Swift on viewDidLoad() self.tableView.tableFooterView = UIView(frame: CGRect.zero) – Kam K Nov 28 '15 at 00:50
6
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Robert Varga
  • 477
  • 1
  • 7
  • 15
  • 1
    Code-only answers, and one-liners even more so, should really have a bit of text explaining what the code does. – hyde Apr 03 '14 at 16:46
  • This solution was already provided over two years previously: http://stackoverflow.com/a/14461000/603977 – jscs Jun 09 '16 at 18:11
3

Use this Code for remove separator line for empty cells.

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

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

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}   
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
3

Just returning an empty UIView() in viewForFooterInSection worked for me:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
codelearner
  • 1,354
  • 1
  • 16
  • 32
2

Please try the following code:

self.tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
if ([self.tblView respondsToSelector:@selector(setSeparatorInset:)])
{
    [self.tblView setSeparatorInset:UIEdgeInsetsZero];
}
// write in view did load
kenorb
  • 155,785
  • 88
  • 678
  • 743
Aatish Javiya
  • 71
  • 1
  • 1
1
tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Mak083
  • 1,160
  • 1
  • 13
  • 33
  • This solution was already provided earlier the same year: http://stackoverflow.com/a/14461000/603977 – jscs Jun 09 '16 at 18:12
0

None of suggested answers were suitable for my similar problem. Implementing this method in tableView delegate finally worked:

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • This disables all separators. Not a solution to the problem at all. Also you call this for every display of cell - really not a good solution. Put this in viewDidLoad if you want to run the code. – nickdnk Jun 17 '15 at 15:43
  • This removes the lines below: that was the core question. If you check the OP's attached image: there in violet it says: "I want to remove those below lines". You can always draw separators on populated cells. And simply puting this in viewDidLoad actually does not work in iOS > 7 due to under the hood bugs in layoutSubviews of UITableViewCell. Even setting the separator color to clearColor of backgroundColor of tableView does not work. Even putting this in cellForRowAtIndexPath does not work... because layoutSubviews happens afterwards: just before cell is displayed. – Rok Jarc Jun 17 '15 at 16:58
  • I just tried it. It removes all separators, as expected, since it's an attribute of the tableView and not the cells. – nickdnk Jun 17 '15 at 17:02
  • There are two types of tableViews: plain and grouped. It works with one type and does not with the other. At least on some versions of iOS. – Rok Jarc Jun 17 '15 at 17:05
0

If you are using Storyboards you can just drag and drop an UIView into your UITableView below your cells and set its height to 0. (Have only tested in an iOS 8 project)

Matoz
  • 137
  • 1
  • 2
  • 8
0

I guess this is what you are looking for.

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.0f;
}
Deepak Badiger
  • 448
  • 7
  • 18
0

You can set the table view to be grouped instead of plain - this changes the look a bit but at least removes the extra lines.

I had this exact problem and ended up changing to grouped views. Looks a lot better.

nickdnk
  • 4,010
  • 4
  • 24
  • 43
0

Some of previous suggestions contain a BIG conceptual error:

if You do:

[cell addSubview: ....

even time a cell is "reused", you will add a new subview for the divider!

avoid it in two ways:

a) use a TAG, and: 1) ask for a subview for that tag let divider = cell.viewWithTag(TAG) ... 2) if present, do NOT add another subview 3) if NOT present add AND tag it.

b) create a custom view and ADD your custom divider in "init" "awakeFromNib" of custom cell.

code for a):

 if let divider = cell.viewWithTag(DIVIDER_TAG) as? UIView{
            // nothing.. eventually change color bases in IndexPath...
        }else{
            let frame = CGRectMake(0, cell.frame.height-1, cell.frame.width, 1)
            divider.tag = DIVIDER_TAG
            divider.backgroundColor = UIColor.redColor()
            cell.addSubview(divider)
        }
ingconti
  • 10,876
  • 3
  • 61
  • 48
0

Inside the cellForRowAtIndexPath

let separatorLineView:UIView = UIView(frame: CGRectMake(0,0,self.tableview.bounds.width,0.5))
separatorLineView.backgroundColor = tableView.separatorColor
cell!.contentView.addSubview(separatorLineView)
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Ronaldo Albertini
  • 1,329
  • 20
  • 24
0

Swift Solution: tableView.tableFooterView = UIView(frame: CGRectZero)

Worked on Xcode 7.2

  • 1
    This solution was already provided over three years ago: http://stackoverflow.com/a/14461000/603977 – jscs Jun 09 '16 at 18:10