I'm using a custom backgroundView
and selectedBackgroundView
for a UITableViewCell
subclass. These cells are in a grouped table, so I'm setting the background and selected background as UIImageView
s based on the cell's row in cellForRowAtIndexPath:
.
The problem I'm having is that when the cell is selected, its selectedBackgroundView
modifies the contents of the contentView
. For example, after selecting and/or highlighting a cell, the UILabel
in the contentView
has its backgroundColor
changes and the UIView
being used as a cell separator is not visible.
Before selection:
After selection:
I don't see this behavior documented anywhere. Is there something I need to do to prevent this? Is there a different approach to showing cell selection/highlighting that I should take to prevent this?
- Note: Since it's a grouped table view, I set a different
backgroundView
s andselectedBackgroundView
s withUIImageView
s to account for the rounded corners on the top and bottom cells in the section incellForRowAtIndexPath:
, but I have the same problem when using the defaultUITableViewSelectionStyleBlue
.
Edit 1:
Per an0's answer, I overrode setHighlighted:animated:
. I'm not sure how reliable the implementation is, but this approach worked to maintain the highlighted
and backgroundColor
properties of the subviews:
NSArray *recursiveAllSubviews = [self recursiveValueForKey:@"subviews"]; // Uses MTRecursiveKVC Cocoapod
NSArray *backgroundColors = [recursiveAllSubviews valueForKey:@"backgroundColor"];
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[recursiveAllSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger index, BOOL *stop){
if ([view respondsToSelector:@selector(setHighlighted:)]) {
[view setValue:[NSNumber numberWithBool:NO] forKey:@"highlighted"];
}
id possiblyNull = [backgroundColors objectAtIndex:index];
if (possiblyNull != [NSNull null]) {
view.backgroundColor = possiblyNull;
}
}];
}