Hide Separator Line on One Uitableviewcell

Hide separator line on one UITableViewCell

in viewDidLoad, add this line:

self.tableView.separatorColor = [UIColor clearColor];

and in cellForRowAtIndexPath:

for iOS lower versions

if(indexPath.row != self.newCarArray.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor redColor];
[cell addSubview:line];
}

for iOS 7 upper versions (including iOS 8)

if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}

Remove separator line for only one cell

On iOS 8 you need to use:

cell.layoutMargins = UIEdgeInsetsZero

If you want to be compatible with iOS 7 as well you should do following:

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}

ADD: If previous didn't work - use this. (from answer below)

 cell.separatorInset = UIEdgeInsetsMake(0, CGFLOAT_MAX, 0, 0);

If none of above worked, you can do:

self.tableView.separatorColor = [UIColor clearColor];

but this will leave 1 pixel empty space, not really removing a line, more making it transparent.

Remove UITableView separator line

Objective-C :

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Swift:

self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

Swift 5.0 renamed it in :

self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none

Apply the line in viewDidLoad() method.

If you want to do it from nib file, set the tableView's Separator property to None

Is there a way to remove the separator line from a single cell in UITableView?

Your best bet is probably to set the table's separatorStyle to UITableViewCellSeparatorStyleNone and manually adding/drawing a line (perhaps in tableView:cellForRowAtIndexPath:) when you want it.

Remove first line separator of UItableView Cell

Within your cellForRowAt delegate function you can use:

if indexPath.row == 0 {
cell?.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}

Remove single separator line from UITableViewCell

The easiest way is: don't use built-in separator lines. Draw the line yourself (as part of the cell) in each row where you want one, and don't draw it (i.e. remove it) in each row where you don't want it.

UITableViewCell hide separator using separatorInset fails in iOS 11

If you are not keen on adding a custom separator to your UITableViewCell I can show you yet another workaround to consider.

How it works

Because the color of the separator is defined on the UITableView level there is no clear way to change it per UITableViewCell instance. It was not intended by Apple and the only thing you can do is to hack it.

The first thing you need is to get access to the separator view. You can do it with this small extension.

extension UITableViewCell {
var separatorView: UIView? {
return subviews .min { $0.frame.size.height < $1.frame.size.height }
}
}

When you have an access to the separator view, you have to configure your UITableView appropriately. First, set the global color of all separators to .clear (but don't disable them!)

override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .clear
}

Next, set the separator color for each cell. You can set a different color for each of them, depends on you.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SeparatorCell", for: indexPath)
cell.separatorView?.backgroundColor = .red

return cell
}

Finally, for every first row in the section, set the separator color to .clear.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
cell.separatorView?.backgroundColor = .clear
}
}

Why it works

First, let's consider the structure of the UITableViewCell. If you print out the subviews of your cell you will see the following output.

; layer = >
<_UITableViewCellSeparatorView: 0x7ff77e4010c0; frame = (15 43.5; 360 0.5); layer = >
>

As you can see there is a view which holds the content, the separator, and the accessory button. From this perspective, you only need to access the separator view and modify it's background. Unfortunately, it's not so easy.

Let's take a look at the same UITableViewCell in the view debugger. As you can see, there are two separator views. You need to access the bottom one which is not present when the willDisplay: is called. This is where the second hacky part comes to play.

structorue of the table view cell

When you will inspect these two elements, you will see that the first (from the top) has a background color set to nil and the second has a background color set to the value you have specified for entire UITableView. In this case, the separator with the color covers the separator without the color.

To solve the issue we have to "reverse" the situation. We can set the color of all separators to .clear which will uncover the one we have an access to. Finally, we can set the background color of the accessible separator to what is desired.

remove seperator line of table view cell in xib

You must remove the separator line in the Attributes inspector of the tableview like in the image below:

Sample Image

Hide remove separator line if UITableViewCells are empty

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.



Related Topics



Leave a reply



Submit