34

I followed Luke Redpath's suggestion here - Selected UItableViewCell staying blue when selected - to deselect the row when returning to the original table view, but I can't get it working. In fact the method doesn't get triggered.

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

I suspect this is because the class isn't a UITableViewController, but a UIViewController with the tableView as a property connected up to the NIB.

How would I get the same behavior - ie deselecting when returning?

Community
  • 1
  • 1
cannyboy
  • 24,180
  • 40
  • 146
  • 252
  • It shouldn't matter as long as the tableView is hooked up correctly. You should be able to put it in `didSelectRowAtIndexPath:` and have it work. You also might want to do a quick NSLog check of what `[self.tableView indexPathForSelectedRow]` returns since it might actually be returning nil. – iwasrobbed Oct 20 '10 at 19:24
  • I used this but had an issue where changing things in the cell (background etc) wasn't reflected immediately. Adding reloadRowsAtIndexPaths: in didDeselectRowAtIndexPath: did the trick – nh32rg Apr 15 '13 at 18:38

7 Answers7

21

why you just don't deselect it in the delegate method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

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

}
ahmad
  • 1,212
  • 1
  • 14
  • 28
  • 2
    You don't have to do `[self.tableView indexPathForSelectedRow]`, indexPath is already a parameter, and you can just insert it there. – Josue Espinosa Apr 14 '14 at 00:24
  • 1
    Also, if you're using the delegate method, you should use the `tableView` parameter instead of `self.tableView`, because they could potentially be different if you have multiple tableviews. – Josue Espinosa Nov 25 '15 at 20:00
17

If you are using a NavigationController, you can do it with its delegate:

- (void)navigationController: (UINavigationController *)navigationController
       didShowViewController: (UIViewController *)viewController
                    animated: (BOOL)animated
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

But you have to check whether the show controller is which you want =/

[EDIT] Create the outlet and link it in IB to NavigationController

@interface MyInt <UINavigationControllerDelegate>
{
    IBOutlet UINavigationController *navigation;
    ...
}
@property (nonatomic, retain) UINavigationController *navigation;
...
@end

@implementation MyInt
@syntesize navigation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigation setDelegate: self];
}
...

@end
grilix
  • 5,211
  • 5
  • 33
  • 34
  • My nav controller is created in my nib, so it has no corresponding class. Is there way around this? – cannyboy Oct 20 '10 at 12:17
  • You can map it to an IBOutlet ivar. – Peter DeWeese Oct 20 '10 at 12:22
  • My setup is that I've got a MainViewController class with a corresponding NIB. That nib has UITabBarController (also an IBOutlet property 'tabBarController' in MainViewController). Within that tab bar are several UINavigation controllers. These are not referenced in code. So I am not sure how to access any of these nav controllers – cannyboy Oct 20 '10 at 12:46
8

I hade the same problem, but after debugging I found out the indexPathForSelectedRow count was 0 so I double checked my code and noticed I was calling

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // some code...
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

After deleting this line it just worked as expected.

FrankWest
  • 365
  • 2
  • 10
  • This was a completely obvious mistake that caught me. I think lots of people including myself put the deselect in the delegate out of habit. – micnguyen Jan 04 '16 at 13:06
8

Are you selecting the row programmatically? If so, I had the same problem when selecting a row using [cellToBeSelected setSelected:YES];.

By using the UITableViewController's selectRowAtIndexPath:animated:scrollPosition: method it kept track of the current selected indexPath.

In fact, if clearsSelectionOnViewWillAppear is set to true, one does not have deselect rows manually.

Alexander Wallin
  • 1,394
  • 10
  • 22
4

Very simple : Try this line.

Add deselectRowAtIndexPath in your didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
2

Swift 3.0

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
    //Write Your Other code Here.
            }

It Will deselect Row after you click on It.

Dhruv Khatri
  • 803
  • 6
  • 15
0
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor grayColor]];
}

-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor clearColor]];
}
sphinks
  • 3,048
  • 8
  • 39
  • 55