禁用背景颜色更改,但在UITableViewCell突出显示时启用文本颜色更改

Raf*_*fal 1 objective-c uitableview ios

我想让UITableViewCell文本颜色在选中时更改,但仅限于此.我想禁用单元格中的背景突出显示.我看到了解决方案:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)

但这也会禁用文本颜色更改.我想以简单的方式实现这种行为吗?

its*_*dra 5

在你的cellForRowAtIndexPath,做:

步骤1.将您的选择样式设置为默认值.

cell.selectionStyle = UITableViewCellSelectionStyleDefault;
Run Code Online (Sandbox Code Playgroud)

步骤2.将空视图设置为背景视图

UIView *view = [UIView new] ;
[view setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:view];
Run Code Online (Sandbox Code Playgroud)

第3步.设置所需的文字颜色

[[cell textLabel] setTextColor:[UIColor orangeColor]];
[[cell textLabel] setHighlightedTextColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.