Created
September 13, 2014 07:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIViewController+DeselectWorkaround.h | |
// | |
// Created by Richard Hult on 2014-09-13. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIViewController (DeselectWorkaround) | |
- (void)deselectWorkaroundForSelector:(SEL)selector; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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