UITableViewCell chose shadow color when deselecting

I have a standard UITableView. I want to set the shadowColor cell to [UIColor whiteColor] , but only when the cell is touched. For this, I use the following code. This is a custom subclass of UITableViewCell 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 when deselecting, the cell has a very short period when both the label text and the shadow are white. See this screenshot at the exact time. Cancel selection:

Shadow example

Basically the same approach as in these two posts:

UILabel shadow of the selected color of the selected cell

Remove text shadow in UITableViewCell when it is selected

I use the accepted answer approach in the last question.

I created a very simple code project and uploaded it to github . This shows my problem. This is just a UITableViewController that displays a single cell.

Also, nothing out of the ordinary. 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?

+4
source share
8 answers

If I understand the problem, you need to display the shadow color until the cell selection is turned off. I'm not sure if the way you tried is wrong, a simpler solution works fine, though.

Note that you will need to remove the observer if you do not need it.

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; } 
+4
source

Add below to your UITableViewDelegate:

 NSIndexPath *selectedIndex; - (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"; if(indexpath == selectedIndex) { cell.textlabel.shadowColor = [UIColor blackColor]; } else { cell.textlabel.shadowColor = [UIColor whiteColor]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView beginUpdates]; selectedIndex = indexpath; [self.tableView endUpdates]; } 
+2
source

This is the best I could do:

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:NO]; [self setShadowColorSelected:selected]; } - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:YES]; [self setShadowColorSelected:highlighted]; } - (void)setShadowColorSelected:(BOOL)selected { [self.textLabel setBackgroundColor:[UIColor clearColor]]; if (selected) { [self.textLabel setTextColor:[UIColor whiteColor]]; [self.textLabel setShadowColor:[UIColor blackColor]]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView transitionWithView:self.textLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ if (selected) { [self.textLabel setTextColor:[UIColor whiteColor]]; [self.textLabel setShadowColor:[UIColor blackColor]]; } else { [self.textLabel setTextColor:[UIColor blackColor]]; [self.textLabel setShadowColor:[UIColor whiteColor]]; } } completion:nil]; } else { [self.textLabel setTextColor:[UIColor blackColor]]; [self.textLabel setShadowColor:[UIColor whiteColor]]; } } 
0
source

In my opinion, you can fix this problem by changing the color of the table using the following code with the desired color

 [tableView setBackgroundColor:[UIColor "GrayColor"]]; 
0
source

Here is an easy way to accomplish what you want:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.textLabel.shadowColor = [UIColor blackColor]; [super touchesBegan:touches withEvent:event]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { self.textLabel.shadowColor = [UIColor whiteColor]; [super touchesEnded:touches withEvent:event]; } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { self.textLabel.shadowColor = [UIColor whiteColor]; [super touchesCancelled:touches withEvent:event]; } 
0
source

I think you need to animate the text / shadow color change with the same duration as the UITableView to animate / deselect animations. In my opinion, you change the color of the text / shadow the moment the tableView starts an animation (dis) showing the selection selection, so that you get what your colors instantly change, and the selection selection takes some time to revive from one state to another

Try something like this:

 __block UIColor *newShadowColor = selected ? [UIColor blackColor] : [UIColor whiteColor]; [UIView animateWithDuration:0.2 animations:^{ /* change your label/shadow color here. */ self.textLabel.shadowColor = newShadowColor; } completion:^(BOOL finished){ /* this is where the cell is no longer selected or highlighted. You may do some additional style changes to your label here */ }]; 
0
source

I had the same problem. All the solutions I was looking at needed a sub-cash / extra code too large.

In the end, I created a second UILabel under the main UILabel to act like a shadow.

Do not set shadows on the main and shadow labels. For the shadow label, set “Normal Color” to what you want your shadow color to be, and set the highlighted color to “Clear Color”.

Obviously, you need to update the shadow label every time you update the main label. Not a big price to pay in many cases.

Hope this helps!

0
source

I also ran into the same problem.

Instead of using the default label, you can use UIButton and your problem will be resolved.

Place the custom button in the cell.

My requirements have been resolved. It can help you.

0
source

Source: https://habr.com/ru/post/1441875/


All Articles