当单元格选择时,UITableView分隔符的颜色会改变

时间:2022-11-21 21:25:11

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

我创建了一个UITableView,但这个分隔符有问题。我对它进行了定制,使它看起来是灰色的,没有嵌入:

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

问题:当选择一行时,分隔符将变为白色。我使用didSelectRowAt、didDeselectRowAt和didHighlightRowAt,但没有什么可做的,当单元格被选中时,它仍然是白色的。请参见下面的示例(在此示例中选择最后一个单元格)……

当单元格选择时,UITableView分隔符的颜色会改变

1 个解决方案

#1


1  

Try this code line in cellForRowAtIndexPathmethod

尝试使用cellForRowAtIndexPathmethod中的代码行

[cell setSelectionStyle: UITableViewCellSelectionStyleNone];

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

这将把none设置为选择样式(此属性的默认值是UITableViewCellSelectionStyleBlue)。

For cell highlight part, use below code in didSelectRowAtIndexPath :

对于单元格突出显示部分,请在didSelectRowAtIndexPath中使用以下代码:

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

Hope this helps.

希望这个有帮助。

#1


1  

Try this code line in cellForRowAtIndexPathmethod

尝试使用cellForRowAtIndexPathmethod中的代码行

[cell setSelectionStyle: UITableViewCellSelectionStyleNone];

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

这将把none设置为选择样式(此属性的默认值是UITableViewCellSelectionStyleBlue)。

For cell highlight part, use below code in didSelectRowAtIndexPath :

对于单元格突出显示部分,请在didSelectRowAtIndexPath中使用以下代码:

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

Hope this helps.

希望这个有帮助。