Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the UITableView separatorColor for a single UITableViewCell

Tags:

iphone

I have a UITableView that uses a variety of custom UITableViewCells.

I'd like to be able to have one of these table cells appear with a different separator color than the rest of the other cells.

I know that tableview.seperatorColor updates the whole tableView. Is there a cell specific property I missing or another approach to doing this?

Thanks for any tips. Sorry if I am just spacing on something in the UITableViewCell class.

like image 451
Nick Avatar asked Sep 14 '10 18:09

Nick


People also ask

Is it possible to add UITableView within a UITableviewCell?

yes it is possible, I added the UITableVIew within the UITableView cell .. :) no need to add tableview cell in xib file - just subclass the UITableviewCell and use the code below, a cell will be created programatically.

How do I change the color of a separator in Swift?

To change the separator color using Interface Builder we need to open the storyboard/xib file. Once storyboard/xib file is open, select the UITableView that you want to change the separator color for. Now that we have selected the UITableView, we need to go to the Attribute Inspector .


2 Answers

Disclaimer - this worked for me at the time under my specific circumstances. It is not guaranteed to work, it appears to no longer work, and I now advise you subclass UITableViewCell.

Came across this post when looking to set the UITableView.separatorColor differently across groups/sections in a grouped UITableView.

You don't necessarily need to subclass UITableViewCell. You can try setting tableView.separatorColor on each call to tableView:cellForRowAtIndexPath:.

For example, if you want the separator to be visible with the default color in the first section, visible in the first row/cell of the second section, and invisible in the rest of the rows in the second section, you can do the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
    case 0:
        tableView.separatorColor = nil;
        break;
    case 1:
        switch (indexPath.row) {
            case 0:
                tableView.separatorColor = nil;
                break;
            case 1:
                tableView.separatorColor = [UIColor clearColor];
                break;
            default:
                break;
        }
        break;
    default:
        break;
}
like image 102
nekno Avatar answered Nov 09 '22 09:11

nekno


The tableView.separatorColor is global across all cells.

If you want to further customize these colors, your best bet would be to set separatorStyle property to UITableViewCellSeparatorStyleNone, and override UITableViewCell.

Then you can draw your own custom seperator in the contentView of the Cell and customize it.

like image 23
davydotcom Avatar answered Nov 09 '22 11:11

davydotcom