UITableViewCell selected shadow color on deselection
ios, ipad, iphone, objective-c, uitableview
Solution
If I understood the problem, you need to display the shadow color until the cell selection is animated to be gone. I'm not sure what is incorrect in the way you tried, more straightforward solution works fine though.
Please note, you'll need to remove observer once it's not needed.
ExampleTableViewCell.h
@interface ExampleTableViewCell : UITableViewCell {
}
- (void) setSelectionShadowOfColor:(UIColor *) selColor;
@end
ExampleTableViewCell.m
@implementation ExampleTableViewCell
- (void) setSelectionShadowOfColor:(UIColor *) selColor {
self.textLabel
[self addObserver:self
forKeyPath:@"textLabel.highlighted" // not isHighlighted as that is a getter name of the highlighted property
options:NSKeyValueObservingOptionNew
context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
BOOL isHighlighted = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
if (isHighlighted) {
self.textLabel.shadowColor = [UIColor blackColor];
} else {
self.textLabel.shadowColor = [UIColor whiteColor];
}
}
@end
ExampleTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ExampleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // note the type ExampleTableViewCell is used here to avoid the interface lookup mess
if (!cell) {
cell = [[ExampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionShadowOfColor:[UIColor blackColor]];
}
cell.textLabel.text = @"test";
return cell;
}
Problem
I have a standard UITableView. I want to set the `shadowColor` of the cell's `textLabel` to `[UIColor whiteColor]`, but only when the cell is touched. For that, I'm using the following code. It's a custom UITableViewCell subclass that overrides setSelected/setHighlighted: ``` @implementation ExampleTableViewCell - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; [self setShadowColorSelected:selected]; } - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:animated]; [self setShadowColorSelected:highlighted]; } - (void)setShadowColorSelected:(BOOL)selected { if (selected) { self.textLabel.shadowColor = [UIColor blackColor]; }else { self.textLabel.shadowColor = [UIColor whiteColor]; } } @end ``` My problem with this approach is that, on deselection, the cell has a very short period where both the label's text and shadow are white. See this screenshot, which was taken in the exact moment of deselection: It's basically the same approach as in these two posts: UILabel shadow from custom cell selected color Removing text shadow in UITableViewCell when it's selected I'm using the approach of the accepted answer in the latter question. I have created a very very simple code project and uploaded it to github. It shows off my problem. It's just a UITableViewController that displays a single cell. Apart from that, there's nothing fancy. UITableView delegate methods: ``` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[ExampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = @"test"; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; //setting this to NO doesn't work either! } ``` Any ideas?