seguedestination seguedestination - 1 year ago 124
iOS Question

How to hide cell separator in a static cell?

I have custom static cells in a

, and I want to remove the cell separator in some specified cell. But how can I programmatically do this in static cells?

My device :iphone 5 iOS 7
IOS simulator:iphone 4s-iphone 6p , all iOS 8.3

Answer Source

Your best bet is probably to set the table's separatorStyle to UITableViewCellSeparatorStyleNone and manually adding/drawing a line (perhaps in tableView:cellForRowAtIndexPath:) when you want it.

Use like this,

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Drawing our own separatorLine here because I need to turn it off for the
    // last row. I can only do that on the tableView and on on specific cells.
    // The y position below has to be 1 less than the cell height to keep it from
    // disappearing when the tableView is scrolled.
    UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

    [separatorLine release];

// Setup default cell setttings.
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;

Set in ViewDidload

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download