Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to remove the separator line from a single cell in UITableView?

I know I can change the UITableView property separatorStyle to UITableViewCellSeparatorStyleNone or UITableViewCellSeparatorStyleSingleLine to change all the cells in the TableView one way or the other.

I'm interested having some cells with a SingleLine Separator and some cells without. Is this possible?

like image 407
CodingWithoutComments Avatar asked Jul 31 '09 15:07

CodingWithoutComments


People also ask

How do I remove a separator from a table view?

To hide UITableViewCell separator completely, simply set it's colour to UIColor. clearColor(). This will make the cell separator not visible.

How do I turn off last cell separator in Swift?

When wanting to hide the separator, use the new class and set myTableView. hideLastSeparator = YES . The way this works is by obstructing the last separator by adding a new view over it.

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.


2 Answers

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.

like image 58
Mike McMaster Avatar answered Oct 31 '22 09:10

Mike McMaster


Following Mike's advice, here is what I did.

In tableView:cellForRowAtIndexPath:

...
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;
...

In viewDidLoad:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
like image 38
hanleyp Avatar answered Oct 31 '22 09:10

hanleyp