Mickael OP
Mickael OP

Reputation: 43

UITableView separator color changing when cell selected

I've created a UITableView and have a problem with the separator. I customized it so that it appears gray and without inset :

self.tableView.separatorInset = .zero
self.tableView.separatorColor = AppColors.separatorColor

I also tried with an image :

self.tableView.separatorInset = .zero
tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
tableView.separatorColor = UIColor(patternImage: #imageLiteral(resourceName: "thin_divider"))

Problem : When a row is selected, the separator becomes white. I played with didSelectRowAt, didDeselectRowAt and didHighlightRowAt but nothing to do, it stays white when the cell is selected. See the example below (last cell is selected on this example)...

example of the problem

Upvotes: 4

Views: 1324

Answers (1)

Mrunal
Mrunal

Reputation: 14118

Try this code line in cellForRowAtIndexPathmethod

[cell setSelectionStyle: UITableViewCellSelectionStyleNone];

This will set none as the selection style (default value for this property is UITableViewCellSelectionStyleBlue).

For cell highlight part, use below code in didSelectRowAtIndexPath :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setHighlighted: NO];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
} 

Hope this helps.

Upvotes: 3

Related Questions