Created
September 13, 2014 07:15
-
-
Save rhult/6770b263489698b5380c to your computer and use it in GitHub Desktop.
Workaround for broken deselection of tableview cells 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
// Using the workaround in a UITableViewController: | |
// If UITableViewController, do this in viewDidLoad: | |
// self.clearsSelectionOnViewWillAppear = NO; | |
// Then add: | |
//- (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 | |
@interface UIViewController (DeselectWorkaround) | |
- (void)deselectWorkaroundForSelector:(SEL)selector; | |
@end | |
// UIViewController+DeselectWorkaround.m | |
#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