Hide separator line deletion if UITableViewCells is empty

I have a UITableView and I only have 3 rows, and I can see these 3 rows. The problem is that the cells are empty: I see the rows there. I do not want to see these lines.

Any idea how to remove these lines?

Below is the image for what I am looking for.

Image link

+80
ios cocoa-touch uitableview
Jan 22 '13 at 14:24
source share
17 answers

You can hide the standard UITableView separator line using any of the code snippets below. The easiest way to add a custom separator is to add a simple UIView from 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 answer , it is very short and useful. And you should also try this single line,

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

Not sure, but it works in all versions of iOS that I checked, with iOS 5 and later, up to iOS 7.

+86
Jan 22 '13 at 14:25
source share

Even simpler than Andrei Z answer: Just create a fake tableFooterView in your UITableView class:

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

and Swift:

tableFooterView = UIView()

+140
Jan 22 '13 at 14:35
source share

A transparent UIView as a UIView footer with a height of 1px 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]; 
+7
Jan 22 '13 at 14:25
source share
 self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
+6
Apr 03 '14 at 16:13
source share

Updated answer for swift and iOS 9. This works for table views of .Plain .

  tableView.tableFooterView = UIView() 
+4
Nov 20 '15 at 18:45
source share

Use this code to remove the separation 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]; } 
+3
Nov 22 '13 at 4:37 on
source share

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 
+2
Mar 02 '15 at 13:11
source share

Just returned an empty UIView() in viewForFooterInSection for me:

 override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { return UIView() } 
+2
Mar 29 '16 at 9:27
source share

you can use

 tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 

It worked for me ...

+1
Nov 26 '13 at 10:09
source share

None of the suggested answers are suitable for my similar problem. The implementation of this method in the tableView delegate finally worked:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { tableView.separatorStyle = UITableViewCellSeparatorStyleNone; } 
0
Nov 10 '14 at
source share

If you use Storiesboards, you can simply drag the UIView into your UITableView under your cells and set its height to 0. (Tested only in iOS 8 project)

0
Feb 06 '15 at 13:01
source share

I think this is what you are looking for.

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 1.0f; } 
0
Jun 04 '15 at 7:53 on
source share

You can customize the table view for grouping instead of simple - this changes the appearance a bit, but at least removes extra rows.

I had this exact problem and switched to grouped views. It looks a lot better.

0
Jul 22 '15 at 11:57
source share

Some of the previous sentences contain a BIG conceptual error:

if you do:

[cell addSubview: ....

even time, when the cell is "reused", you will add a new view for the separator!

avoid it in two ways:

a) use TAG and: 1) ask for the subtitle for this tag let divider = cell.viewWithTag (TAG) ... 2) if present, DO NOT add another view 3) if NOT present, add AND mark it.

b) create a custom view and add your custom separator to the "init" "awakeFromNib" of the custom cell.

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) } 
0
Dec 31 '15 at 18:53
source share

Inside cellForRowAtIndexPath

 let separatorLineView:UIView = UIView(frame: CGRectMake(0,0,self.tableview.bounds.width,0.5)) separatorLineView.backgroundColor = tableView.separatorColor cell!.contentView.addSubview(separatorLineView) 
0
Apr 08 '16 at 20:11
source share

Quick fix: tableView.tableFooterView = UIView (frame: CGRectZero)

Powered by Xcode 7.2

0
May 11 '16 at 1:07 a.m.
source share

Add the following line to the UITableViewDataSource numberOfRowsInSection: method UITableViewDataSource numberOfRowsInSection:

 tableView.separatorColor = [UIColor clearColor]; 
-3
Feb 07 '14 at 4:55
source share



All Articles