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?