Instantly share code, notes, and snippets.

Embed
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];
}
}];
}
}
@mikeabdullah

This comment has been minimized.

Show comment
Hide comment
@mikeabdullah

mikeabdullah Sep 24, 2014

The blog post associated with this gist is http://blog.supertop.co/post/80781694515/viewmightappear in case anyone arrives here directly

mikeabdullah commented Sep 24, 2014

The blog post associated with this gist is http://blog.supertop.co/post/80781694515/viewmightappear in case anyone arrives here directly

@mikeabdullah

This comment has been minimized.

Show comment
Hide comment
@mikeabdullah

mikeabdullah Sep 24, 2014

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

mikeabdullah commented Sep 24, 2014

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

@winkelsdorf

This comment has been minimized.

Show comment
Hide comment
@winkelsdorf

winkelsdorf Sep 6, 2015

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)
        }
    })
}

winkelsdorf commented Sep 6, 2015

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