Skip to content

Instantly share code, notes, and snippets.

@rhult
Created September 13, 2014 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhult/46ee6c4e8a862a8e66d4 to your computer and use it in GitHub Desktop.
Save rhult/46ee6c4e8a862a8e66d4 to your computer and use it in GitHub Desktop.
Workaround for broken behavior with table view cell deselection when swiping back in navigation
- (void)viewDidLoad
{
[super viewDidLoad];
// If UITableViewController, otherwise comment out (make sure to name your tableview ivar tableView if so).
self.clearsSelectionOnViewWillAppear = NO;
}
// Use the workaround:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self deselectWorkaroundForSelector:_cmd];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self deselectWorkaroundForSelector:_cmd];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self deselectWorkaroundForSelector:_cmd];
}
//
// UIViewController+DeselectWorkaround.h
//
// Created by Richard Hult on 2014-09-13.
//
#import <UIKit/UIKit.h>
@interface UIViewController (DeselectWorkaround)
- (void)deselectWorkaroundForSelector:(SEL)selector;
@end
//
// UIViewController+DeselectWorkaround.m
//
// Created by Richard Hult on 2014-09-13.
//
#import "UIViewController+DeselectWorkaround.h"
#import <objc/runtime.h>
@interface UIViewController (DeselectWorkaroundTableView)
- (UITableView *)tableView;
@end
@implementation UIViewController (DeselectWorkaround)
static char savedSelectedIndexPathKey;
- (void)deselectWorkaround_setSavedSelectedIndexPath:(NSIndexPath *)indexPath
{
objc_setAssociatedObject(self, &savedSelectedIndexPathKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSIndexPath *)deselectWorkaround_savedSelectedIndexPath
{
return objc_getAssociatedObject(self, &savedSelectedIndexPathKey);
}
- (UITableView *)deselectWorkaround_tableView
{
if ([self respondsToSelector:@selector(tableView)]) {
return [self tableView];
}
return nil;
}
- (void)deselectWorkaroundForSelector:(SEL)selector
{
if (selector == @selector(viewDidAppear:)) {
[self deselectWorkaround_viewDidAppear];
}
else if (selector == @selector(viewWillDisappear:)) {
[self deselectWorkaround_viewWillDisappear];
}
else if (selector == @selector(viewWillAppear:)) {
[self deselectWorkaround_viewWillAppear];
}
}
- (void)deselectWorkaround_viewDidAppear
{
[self deselectWorkaround_setSavedSelectedIndexPath:nil];
}
- (void)deselectWorkaround_viewWillDisappear
{
if (self.deselectWorkaround_savedSelectedIndexPath) {
[self.deselectWorkaround_tableView selectRowAtIndexPath:self.deselectWorkaround_savedSelectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
- (void)deselectWorkaround_viewWillAppear
{
[self deselectWorkaround_setSavedSelectedIndexPath:self.deselectWorkaround_tableView.indexPathForSelectedRow];
if (self.deselectWorkaround_savedSelectedIndexPath) {
[self.deselectWorkaround_tableView deselectRowAtIndexPath:self.deselectWorkaround_savedSelectedIndexPath animated:YES];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment