Remove separator line for only one cell

后端 未结 13 2240
萌比男神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 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
    

提交回复
热议问题