Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Custom viewWillApear to restore selected row when transition is cancelled
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedRowIndexPath) {
[self.tableView deselectRowAtIndexPath:selectedRowIndexPath animated:YES];
[[self transitionCoordinator] notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if ([context isCancelled]) {
[self.tableView selectRowAtIndexPath:selectedRowIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

The blog post associated with this gist is in case anyone arrives here directly

One tweak you may wish to do, is only kick in this logic if (self.clearsSelectionOnViewWillAppear)

Great, and here is the Swift Version which also takes the above mentioned clearsSelectionOnViewWillAppear into account:

if clearsSelectionOnViewWillAppear, let selectedRowIndexPath = tableView.indexPathForSelectedRow() {
    tableView.deselectRowAtIndexPath(selectedRowIndexPath, animated:true)
    transitionCoordinator()?.notifyWhenInteractionEndsUsingBlock({context -> Void in
        if context.isCancelled() {
            self.tableView.selectRowAtIndexPath(selectedRowIndexPath, animated:false, scrollPosition:UITableViewScrollPosition.None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment