Fabio's answer works well, but does not give the right look if the user goes through a bit and then changes his mind. To qualify for this case, you need to save the selected pointer path and reset if necessary.
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.savedSelectedIndexPath = nil; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.savedSelectedIndexPath) { [self.tableView selectRowAtIndexPath:self.savedSelectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.savedSelectedIndexPath = self.tableView.indexPathForSelectedRow; if (self.savedSelectedIndexPath) { [self.tableView deselectRowAtIndexPath:self.savedSelectedIndexPath animated:YES]; } }
If you are using the UITableViewController, be sure to disable the built-in cleanup:
self.clearsSelectionOnViewWillAppear = NO;
and add property for savedSelectedIndexPath:
@property(strong, nonatomic) NSIndexPath *savedSelectedIndexPath;
If you need to do this in several different classes, it may make sense to separate it in an assistant, for example, as in this context: https://gist.github.com/rhult/46ee6c4e8a862a8e66d4
Rhult Sep 13 '14 at 7:30 2014-09-13 07:30
source share