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?

6
  • 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
    Commented Jul 1, 2015 at 20:03
  • Yes. I'm trying to hide the separator at row index 1
    – Jessica
    Commented Jul 1, 2015 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
    Commented Jul 1, 2015 at 20:14
  • Can you please provide an example. Also In the storyboard, should I set separator to none?
    – Jessica
    Commented Jul 1, 2015 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
    Commented Jul 1, 2015 at 21:23

14 Answers 14

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.

7
  • Will the second method support ios 8 as well?
    – Jessica
    Commented Jul 1, 2015 at 20:11
  • first one is for iOS 7 and lower, second one is for iOS 8. You need to put both into your app, so it will run proper code for given iOS version Commented Jul 1, 2015 at 20:13
  • (In the answer you wrote the first one is for io8.) Do I need to have an if statement to support both versions?
    – Jessica
    Commented Jul 1, 2015 at 20:14
  • respondsToSelector takes care of checking if the method exists, which is compatibility check Commented Jul 1, 2015 at 21:07
  • 3
    None of these solutions have any effect in my case Commented Sep 17, 2017 at 16:54
20

You can just visually hide separator through separatorInset property:

tableViewCell.separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)
0
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
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.

5
  • 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
    Commented Jan 6, 2017 at 20:06
  • 3
    Hackiest of the solutions. Commented Feb 8, 2017 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. Commented Mar 2, 2017 at 23:39
  • 1
    This is very hacky and will probably break in a future release of iOS.
    – Oscar
    Commented Jul 2, 2020 at 0:11
  • This is using a private API, and even if it works, there is a risk that your application will be rejected
    – IrelDev
    Commented Feb 1, 2021 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);
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)
}
1
  • This seems to be hack..but not recommended to for ideal programming. Commented Oct 14, 2018 at 10:58
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)
    }
}
1
cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)
1
  • 2
    Although this may answer the question, some context and/or explanation would be nice. For future readers also.
    – VDWWD
    Commented Feb 11, 2017 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
            }
        }
    }
}

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
0

Swift 5.1 -

override public func layoutSubviews() {
    super.layoutSubviews()
    subviews.forEach { (view) in
        if type(of: view).description() == "_UITableViewCellSeparatorView" {
            view.isHidden = true
        }
    }
}
1
  • 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
    Commented Jan 8, 2020 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)
    }
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

-16

Swift 3 remove separator all cell:

 override func viewDidLoad() {
        self.yourTableView.separatorStyle = UITableViewCellSeparatorStyle.none
    }
3
  • 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 How to Answer. source
    – Jed Fox
    Commented Jun 2, 2017 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. Commented Nov 28, 2017 at 20:12
  • this code simply removes all separators from all cells on the entire tableView the answer is not correct.
    – Mor4eza
    Commented Feb 14 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.