1

For some reason after coming back to my parent UITableViewController from child UIViewController, the selected cell stays highlighted. I've tried the obvious solution below without any luck:

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

I narrowed the issue down to the following code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selectedString = [self tableView:tableView cellForRowAtIndexPath:indexPath].textLabel.text;

    if([selectedString isEqualToString:@"Browser"])
    {
        [self performSegueWithIdentifier:@"showBrowser" sender:indexPath];
    }
    else if([selectedString isEqualToString:@"Settings"])
    {
        if([[User user] loggedIn])
        {
            [self performSegueWithIdentifier:@"showUserSettings" sender:indexPath];
        }
        else
        {
            [self performSegueWithIdentifier:@"showLogin" sender:indexPath];
        }
    }
    else if([selectedString isEqualToString:@"Blank Page"])
    {
        [self performSegueWithIdentifier:@"showBrowser" sender:[NSURL URLWithString:@"about:blank"]];
    }
}

Removing everything in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath and putting just a single performSegueWithIdentifier method WILL deselect the cell once coming back from the child, however what I have now does not deselect it.

I have also tried placing

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

in the didSelectRowAtIndexPath method itself with no luck.

Any ideas?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3397214
  • 187
  • 3
  • 10

5 Answers5

3

In your UITableViewController implement viewWillAppear delegate.

In viewWillAppear write this line:

[self.tableView reloadData];

Hope this helps.. :)

Edit:

If you reload a cell that cell will be deselected. This and this will definitely help you.

Community
  • 1
  • 1
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Thanks for the reply, but unfortunately none of the posts on your edit worked either. – user3397214 Mar 09 '14 at 07:27
  • Awesome. I was using a TableViewCell subclass and the highlighted state would stay up if the user slid their finger away from the cell. This worked perfectly when I stuck it in didSelectRowAtIndexPath. – mondousage Jul 10 '15 at 15:31
  • @mondousage > Glad to know.. :) – Rashad Jul 11 '15 at 04:01
1

implement didSelectRowAtIndexPath and write [tableView deselectRowAtIndexPath:indexPath];

Mohd Iftekhar Qurashi
  • 2,385
  • 3
  • 32
  • 44
0

Try adding [cell setSelected:NO animated:YES] in the didSelectRowAtIndexPath method.

aksh1t
  • 5,410
  • 1
  • 37
  • 55
  • This works, however it removes the grey line separating it from the cell beneath it. Also, when selecting the cell a second time, it doesn't highlight at all. Any workaround to this? – user3397214 Mar 09 '14 at 05:43
  • Sorry for the late reply, but this code is working perfectly for me. The grey separator line remains there (for the default cells). Try removing `[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];` from your `viewWillAppear`. – aksh1t Mar 10 '14 at 06:21
0

Try:

 [self.tableView reloadData];

In ViewWillApear

MCMatan
  • 8,623
  • 6
  • 46
  • 85
0

Swift 3.1

In func tableView(didSelectRowAt indexPath) insert:

tableView.deselectRow(at: indexPath, animated: true)

CWiley
  • 251
  • 3
  • 11