How to remove the last border of the last cell in a UITableView?

In my application, I use UITableView My problem is that I want to remove the last border of the last cell in the UITableView .

Please check the following image:

enter image description here

+64
ios objective-c iphone xcode uitableview
Aug 28 '12 at 21:48
source share
17 answers

Updated 9/14/15. My original answer is deprecated, but it is still a universal solution for all versions of iOS:

You can hide the standard tableView dividing line and add your custom row at the top of each cell. 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, cell.bounds.size.width, 1)]; separatorLineView.backgroundColor = [UIColor grayColor]; [cell.contentView addSubview:separatorLineView]; 

Today, I subscribe to another way to hide additional separators below the cells (works for iOS 6.1 +):

 self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
+21
Aug 28 2018-12-12T00:
source share
β€” -

The best solution is to add a footer. The following code perfectly hides the last row of the cell:

Objective-c

 self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)]; 

Swift 4.0

 tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1)) 
+116
Nov 12 '14 at 13:22
source share

IOS 7 has a simpler solution. The intended cell is your last cell:

 cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0); 
+39
Nov 04 '13 at 12:21
source share

This works for me on iOS 7 and 8:

 cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds)); 

NOTE. Be careful when using tableView.bounds , as it sometimes reports an incorrect value depending on when you call it. See Report Incorrect Boundaries in Landscape Mode

+13
Jun 10 '14 at 20:37
source share

No, you cannot do this. These separators are pretty carved and dry, style and color like him. However, an alternative solution could be to completely remove the separators and set the background image with a horizontal line at the bottom for all cells except the last.

EX:

  [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; if (indexPath.row == [myArray count] - 1) { UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]]; [cell setBackgroundView:imageView]; } 
+2
Aug 28 2018-12-12T00:
source share

Here is the tool that I am currently using. this may not be the best approach, but it works.

Remove SeparatorColor and setSeparatorStyle to create a custom separator

 - (void)viewDidLoad { [super viewDidLoad]; ... [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; [self.tableView setSeparatorColor:[UIColor clearColor]]; } 

Add custom separator to each cell with backgroundview background

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; ... UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-1, 320, 1)]; separatorLineView.backgroundColor = self.tableView.backgroundColor; [cell.contentView addSubview:separatorLineView]; return cell; } 
+2
Apr 23 '14 at 0:15
source share

My shorter version:

 self.tblView.tableFooterView = [UIView new]; 
+2
Apr 28 '16 at 9:23
source share

Add this line of code to viewDidLoad() .

 tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001)) 

This ensures that the last separator is replaced and that replacing the (invisible) footer does not take up any extra height. Since the width of the footer view will be controlled using a UITableView , so you can set it to 0.

+2
Aug 10 '17 at 3:11
source share

For iOS 7 and above

  -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { BOOL bIsLastRow = NO; //Add logic to check last row for your data source NSDictionary *dict = [_arrDataSource objectAtIndex:indexPath.section]; if([_arrDataSource lastObject] == dict) { bIsLastRow = YES; } //Set last row separate inset left value large, so it will go outside of view if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsMake(0, bIsLastRow ? 1000 :0, 0, 0)]; } } 
+1
Jul 01 '15 at 20:18
source share

In Swift, it's just:

 tableView.tableFooterView = UIView() 
+1
Sep 29 '16 at 21:44
source share

The code below helps me remove the separator from the last line in a UITableView.

Swift 3.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) { cell.separatorInset.right = cell.bounds.size.width } }

+1
May 18 '17 at 21:16
source share

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

0
Dec 16 '14 at 12:15
source share

Find the last cell index in the cellForRow cellForRow and place the line of code below:

 cell.separatorInset = UIEdgeInsetsMake( 0.f, 40.0f, 0.f, self.view.frame.size.width-40.0f ); 
0
Jun 16 '16 at 8:14
source share

Another option is to subclass UITableView :

 @synthesize hideLastSeparator = _hideLastSeparator; @synthesize hideLastSeparatorView = _hideLastSeparatorView; -(void)setHideLastSeparator:(BOOL)hideLastSeparator { if (_hideLastSeparator == hideLastSeparator) { return; } _hideLastSeparator = hideLastSeparator; if (_hideLastSeparator) { _hideLastSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentSize.height, self.bounds.size.width, 0.5f)]; _hideLastSeparatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; _hideLastSeparatorView.backgroundColor = self.backgroundColor; [self addSubview:_hideLastSeparatorView]; [self hideSeparator]; } else { [_hideLastSeparatorView removeFromSuperview]; _hideLastSeparatorView = nil; } } -(void)setContentSize:(CGSize)contentSize { [super setContentSize:contentSize]; if (_hideLastSeparator) { [self hideSeparator]; } } -(void)hideSeparator { CGRect frame = _hideLastSeparatorView.frame; frame.origin.y = self.contentSize.height - frame.size.height; _hideLastSeparatorView.frame = frame; } 

.h should only contain property declarations for hideLastSeparator and hideLastSeparatorView .
If you want to hide the delimiter, use the new class and set myTableView.hideLastSeparator = YES .
The way this works is to prevent the last separator by adding a new look to it.

This is somewhat easier to use in my opinion (than using a custom separator or setting the last Inset separator of the last cell) and avoids some strange animations that the tableFooterView setup method sometimes calls (for example, during insert / delete rows or other table animations).

0
Aug 24 '16 at 11:10
source share

Swift 4.2

To get the separator that goes from left to right in your last cell, add it to tableView(_:cellForRowAt:) your UITableViewDataSource's tableView(_:cellForRowAt:) :

 if tableView.numberOfRows(inSection: indexPath.section) - 1 == indexPath.row { cell.separatorInset = .zero } 

If you do not want to use a separator for one specific cell, consider creating your own separator and set tableView.separatorStyle =.none .

0
Aug 24 '18 at 13:17
source share

Extensibility solution for Swift 4, tested on iOS 12

I noticed that setting the empty view height = 1 also removes the separator of the last visible cell, however setting height = 0 just removes the separator of empty cells

 extension UITableView { func removeSeparatorsOfEmptyCells() { tableFooterView = UIView(frame: .zero) } func removeSeparatorsOfEmptyCellsAndLastCell() { tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 1))) } } 
0
Jan 21 '19 at 13:15
source share

Having deployed to Tonny Xu, I have several partitions, and this seems to work to remove the last cell separator

 if(indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1) { self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)]; } 

In the source cellForRowAtIndexPath datasource

-2
Dec 04 '14 at 15:29
source share



All Articles