39

I'm trying to remove the separator for one UITableViewCell. I did the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    if (indexPath.row == 1)
        cell.separatorInset = UIEdgeInsetsZero;

    return cell;
}

(It's static cells.) When I run the app. The separator line is still there. How can I remove the separator line for one cell?

Jessica
  • 9,379
  • 14
  • 65
  • 136
  • Setting separatorInset to UIEdgeInsetsZero is to show the separator the with from the left to right. Do you want to hide the separatorInset only at the row index 1? – thanyaj Jul 01 '15 at 20:03
  • Yes. I'm trying to hide the separator at row index 1 – Jessica Jul 01 '15 at 20:04
  • removing only 1 separator on the cell is tricky because you either get all seperator inset or none. I usually ended up implement my own separator view that is just a uiview with 1 pixel height. Then, design to show and hide the separator view. There may be some better ideas. which Im keen to learn us well. – thanyaj Jul 01 '15 at 20:14
  • Can you please provide an example. Also In the storyboard, should I set separator to none? – Jessica Jul 01 '15 at 20:52
  • Yes. if you want to have a custom separator view, you need to set separator to none. For the example, it will take a while to write it up. If you could wait, i'll try to put thing together roughly. – thanyaj Jul 01 '15 at 21:23
  • I posted code as an answer. hope it help. – thanyaj Jul 01 '15 at 22:14

14 Answers14

25

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.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
20

You can just visually hide separator through separatorInset property:

tableViewCell.separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)
Valentin Shergin
  • 7,166
  • 2
  • 50
  • 53
15

Swift 4

iOS 11

Assign next values for specific cell you need for customization.

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, .greatestFiniteMagnitude)
cell.directionalLayoutMargins = .zero
dimpiax
  • 12,093
  • 5
  • 62
  • 45
6

If you want to hide the separation line for only a specific type of cell, you can use the following code:

 override func layoutSubviews() {
    super.layoutSubviews()
    subviews.forEach { (view) in
        if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
            view.hidden = true
        }
    }
}

Write this code in the cell (it must be a subclass of UITableViewCell) for which you do not want a separation line to appear.

  • 2
    I tried a bunch of other solutions and this ended up being the least hacky. In Swift 3.0, this is the correct syntax: `subviews.forEach { view in if type(of: view).description() == "_UITableViewCellSeparatorView" { view.isHidden = true } }` – gokeji Jan 06 '17 at 20:06
  • 3
    Hackiest of the solutions. – Iulian Onofrei Feb 08 '17 at 15:06
  • Warning - this causes issues in iOS 10. iOS seems to clone separator properties between cell instances, and completely different cell classes on different tableviews start appearing with no separator. – Jordan Smith Mar 02 '17 at 23:39
  • 1
    This is very hacky and will probably break in a future release of iOS. – Oscar Jul 02 '20 at 00:11
  • This is using a private API, and even if it works, there is a risk that your application will be rejected – IrelDev Feb 01 '21 at 11:20
5

Swift 5 Enjoy

//MARK:- In which row you want to hide 
cell.separatorInset = UIEdgeInsets(top: 0, left: CGFloat.greatestFiniteMagnitude, bottom: 0, right: 0);
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
4

Just add following to the cell you don't want separator (swift 3) :

override func awakeFromNib() {
    super.awakeFromNib()

    // remove separator
    self.separatorInset = UIEdgeInsetsMake(0, 1000, 0, 0)
}
COzkurt
  • 427
  • 4
  • 4
1

For those people struggling with this for iOS 9 the solution is a little different than the answers already given. Here is how I solved it in Swift:

override func viewWillAppear(animated: Bool) {
    table.cellLayoutMarginsFollowReadableWidth = false
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if IS_THE_CELL_WITH_NO_SEPARATOR{
        cell.separatorInset = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
    }
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if IS_THE_CELL_WITH_NO_SEPARATOR{
        let size = self.view.bounds.size
        let rightInset = size.width > size.height ? size.width : size.height
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, rightInset)
    }
}
boidkan
  • 4,691
  • 5
  • 29
  • 43
1
cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)
katwal-Dipak
  • 3,523
  • 2
  • 23
  • 23
  • 2
    Although this may answer the question, some context and/or explanation would be nice. For future readers also. – VDWWD Feb 11 '17 at 17:14
1

I created a Extension to UITableViewCell, setting separatorInset value brings anchor bugs to me, I'm using Eureka form Pod.

extension UITableViewCell {
    func hideSeparator(hide: Bool) {
        let subviews = self.subviews
        for view in subviews {
            if view.classForCoder.description() == "_UITableViewCellSeparatorView" {
                view.isHidden = hide
            }
        }
    }
}

Narlei Moreira
  • 256
  • 2
  • 10
0

Another way that is a bit hacky is to create the custom table view cell with a uiView that acts like separator inset. Then, hide and show that when you want to.

I created SampleTableViewCell and a nib file with label and separatorLineView

@interface SampleTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *separatorLineView;
@end

Then, in ViewController Class

@interface ViewController () 
@property (nonatomic) NSArray *items;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.items = @[@"A", @"B", @"C"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SampleTableViewCell" bundle:nil] forCellReuseIdentifier:@"SampleCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SampleTableViewCell *cell = (SampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SampleCell" forIndexPath:indexPath];
    cell.titleLabel.text = self.items[indexPath.row];
    if (indexPath.row == 1) {
        cell.separatorLineView.hidden = YES;
    } else {
        cell.separatorLineView.hidden = NO;
    }
    return cell;
}

@end
thanyaj
  • 302
  • 1
  • 6
0

Swift 5.1 -

override public func layoutSubviews() {
    super.layoutSubviews()
    subviews.forEach { (view) in
        if type(of: view).description() == "_UITableViewCellSeparatorView" {
            view.isHidden = true
        }
    }
}
Yash
  • 227
  • 1
  • 4
  • 1
    Not a good idea. Accessing a private API could get your app disapproved. This name could also change in the future without you knowing. – Drew Jan 08 '20 at 23:44
0

If you'd like to show separator to only the visible cell you can try this:

tableView.separatorInset = UIEdgeInsets(top: 0, left: 1000, bottom: 0, right: 0)

let visibleCell = tableView.visibleCells

    for cellV in visibleCell{
        cellV.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
Eli
  • 181
  • 3
  • 12
0

Make UITableView style is .plain

use below line of code in

cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method

Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51
-16

Swift 3 remove separator all cell:

 override func viewDidLoad() {
        self.yourTableView.separatorStyle = UITableViewCellSeparatorStyle.none
    }
Giang
  • 3,553
  • 30
  • 28
  • Please use the [edit] link to explain how this code works and don't just give the code, as an explanation is more likely to help future readers. See also [answer]. [source](http://stackoverflow.com/users/5244995) – Jed Fox Jun 02 '17 at 14:25
  • More importantly, this answer doesn't answer the question asked. This code simply disables the separators on the entire table; not selectively on a single cell at a time. – SimplePanda Nov 28 '17 at 20:12