Remove separator line for only one cell

后端 未结 13 2109
萌比男神i
萌比男神i 2020-12-30 19:38

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellFo         


        
相关标签:
13条回答
  • 2020-12-30 19:54
    cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)
    
    0 讨论(0)
  • 2020-12-30 19:56

    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.

    0 讨论(0)
  • 2020-12-30 19:59

    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.

    0 讨论(0)
  • 2020-12-30 20:03

    Swift 5 Enjoy

    //MARK:- In which row you want to hide 
    cell.separatorInset = UIEdgeInsets(top: 0, left: CGFloat.greatestFiniteMagnitude, bottom: 0, right: 0);
    
    0 讨论(0)
  • 2020-12-30 20:05

    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 讨论(0)
  • 2020-12-30 20:07

    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 讨论(0)
提交回复
热议问题