I\'m trying to remove the separator for one UITableViewCell
. I did the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellFo
cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)
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.
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.
Swift 5 Enjoy
//MARK:- In which row you want to hide
cell.separatorInset = UIEdgeInsets(top: 0, left: CGFloat.greatestFiniteMagnitude, bottom: 0, right: 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
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
}
}
}
}