seguedestination
seguedestination

Reputation: 419

How to hide cell separator in a static cell?

I have custom static cells in a UITableView, 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

Upvotes: 2

Views: 1000

Answers (4)

Konstantin.Efimenko
Konstantin.Efimenko

Reputation: 1450

Make link from your static cell to UITableViewController and:

- (void)viewDidLoad {
    [super viewDidLoad];
    yourStaticCell.separatorInset = UIEdgeInsetsMake(0.f, yourStaticCell.bounds.size.width, 0.f, 0.f);
}

Upvotes: 0

BKjadav
BKjadav

Reputation: 543

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; 

Upvotes: 2

Vizllx
Vizllx

Reputation: 9246

Just use this code in - cellForRowAtIndexPath: method,

 cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);

If you want for a specific cell:-

if(indexpath.row==1) //desired row number on which you don't want separator
{
 cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
} 

Upvotes: 1

David Berry
David Berry

Reputation: 41226

Since separator style is a property of the table, and not the cell, the easiest way is going to be to hide all of the separators:

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

and then explicitly draw a separator in the cells that need one. Typically the easiest way to do that is to add a one-pixel high subview at the bottom (or top) of the cell and set it's background color to black.

Upvotes: 0

Related Questions