Change the selected cell background colour using UIAppearance
Asked Answered
B

3

1

I need to change the selected cell background colour for all the cells in my app. As I know there is a way to use UIAppearance protocol for this purposes. Is it possible to realize this by the category for UITableViewCell?

Bisulfate answered 18/3, 2014 at 10:38 Comment(0)
O
0

You can't do this direct to UITableViewCell, but you can do it for its contentView:

[[UIView appearanceWhenContainedIn:[UITableViewCell class], nil] setBackgroundColor:[UIColor redColor]];

Note that it will change all the subViews bg color.

Another option is writing a category or subclass the UITableViewCell with UI_APPEARANCE_SELECTOR mark, check this question:

iOS: Using UIAppearance to define custom UITableViewCell color

Oldster answered 18/3, 2014 at 11:25 Comment(3)
This appears to change all cell background, regardless of selected state or not.Emalee
This looks nice, but can you use UIAppearance for only the selected state of a UITableViewCell?Patel
@CliftonLabrum I added an answer below that might answer your question.Elwell
G
4

Using appearance proxy you can colour all cells. Don't know if you can target specific category.

To do the colouring put following code in your AppDelegate.m file:

Put [self customCellBackground]; in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions

and somewhere at the end:

- (void)customCellBackground {
UIView *cellBackgroundView =[[UIView alloc] init];
cellBackgroundView.backgroundColor = [UIColor blackColor];
[[UITableViewCell appearance] setSelectedBackgroundView:cellBackgroundView];}
Gunshy answered 18/3, 2014 at 11:18 Comment(1)
Wouldn't recommend chucking stuff like this into the AppDelegate. At least wrap it in a class which handles global styling and let the AppDelegate call that on startup.Quaquaversal
E
3

As null's answer is not for selected cell backgrounds and Armands L.'s answer did not work consistently for me (selecting cells by 'user-tap' did work, but programmatical cell selection showed strange results (like sometimes the selected background was not visible, or did not fill the cell's height properly...).

I found a custom solution that worked:

  1. Subclass UITableViewCell
  2. Initialize self.selectedBackgroundView in init and
  3. Add custom UIColor property with UI_APPEARANCE_SELECTOR for custom selected background color

.h file:

@property (nonatomic) UIColor* selectedCellBackgroundColor UI_APPEARANCE_SELECTOR;

.m file:

in init method(s):

self.selectedBackgroundView = [[UIView alloc] init];

and last but not least the setter function for the color:

- (void) setSelectedCellBackgroundColor:(UIColor*) color {
    _selectedCellBackgroundColor = color;
    self.selectedBackgroundView.backgroundColor = color;
}
Elwell answered 25/8, 2015 at 15:53 Comment(0)
O
0

You can't do this direct to UITableViewCell, but you can do it for its contentView:

[[UIView appearanceWhenContainedIn:[UITableViewCell class], nil] setBackgroundColor:[UIColor redColor]];

Note that it will change all the subViews bg color.

Another option is writing a category or subclass the UITableViewCell with UI_APPEARANCE_SELECTOR mark, check this question:

iOS: Using UIAppearance to define custom UITableViewCell color

Oldster answered 18/3, 2014 at 11:25 Comment(3)
This appears to change all cell background, regardless of selected state or not.Emalee
This looks nice, but can you use UIAppearance for only the selected state of a UITableViewCell?Patel
@CliftonLabrum I added an answer below that might answer your question.Elwell

© 2022 - 2024 — McMap. All rights reserved.