0

I have a custom subclass of UITableViewCell. I am customizing the labels contained within the cell by setting some attributes of UILabel's appearance proxy, as follows:

[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil].textColor = [UIColor blackColor];
[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil].highlightedTextColor = [UIColor redColor];

When I select the cell in the UI, the labels' colors change as expected and the code performs a push segue to the next view controller. However, when I pop this new view controller and go back to the screen with the custom UITableViewCells, the highlighted text color remains even though I am deselecting the cell in the code as follows:

- (void)viewDidAppear:(BOOL)animated
{
    if ([self.tableView indexPathForSelectedRow]) {
        [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
    }
    [super viewDidAppear:animated];
}

Is this a bug in iOS 7 or am I doing something wrong?

Jamie Forrest
  • 10,895
  • 6
  • 51
  • 68

2 Answers2

0

I won't call it a bug but there is a way to fix it.

-(void)viewWillDisappear:(BOOL)animated{

  [super viewWillDisappear:YES];

  if ([self.tableView indexPathForSelectedRow]) {
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]    animated:YES];
  }

}
Roshan
  • 521
  • 4
  • 16
0

Jamie, I faced the exact same situation. I have a UIView as contentView of UITableViewCell and the UITableView is in a UIPopoverController. The selection highlight wouldn't clear if I reopen the popover after dismissing it.

I did try both the solutions mentioned here and as you say both do not work. While debugging I realized UITableView somehow loses the selection and returns nothing for indexPathForSelectedRow.

Unfortunately I did not find a very good solution. I would call this an iOS 7 bug as UITableView does not behave as before and as expected.

The solution that I employed was to change background color of my UIView instead of using UITableView's selection methods and change it back to default color in viewWillDisappear.

I am all eyes to read a better solution.

zambrey
  • 1,452
  • 13
  • 21