Last active
August 29, 2015 13:57
-
-
Save zuccoi/9781145 to your computer and use it in GitHub Desktop.
Mix-in protocol that keeps selected table view cell during interactive pop
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
/* | |
RLRTableViewCellSelectionDuringInteractivePopAndPush.h | |
Copyright ©2014 Kazki Miura. All rights reserved. | |
*/ | |
#import <UIKit/UIKit.h> | |
@protocol RLRTableViewCellSelectionDuringInteractivePopAndPush | |
@required | |
- (UITableView*)tableView; | |
@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
/* | |
RLRTableViewCellSelectionDuringInteractivePopAndPush.m | |
Copyright ©2014 Kazki Miura. All rights reserved. | |
*/ | |
#import "RLRTableViewCellSelectionDuringInteractivePopAndPush.h" | |
#import "REKit.h" | |
// Constants | |
static void *kLoadedKey = @"RLRTableViewCellSelectionDuringInteractivePopAndPush_loaded"; | |
static NSString *const kBlockKey = @"RLRTableViewCellSelectionDuringInteractivePopAndPushBlock"; | |
@implementation UIViewController (RLRTableViewCellSelectionDuringInteractivePopAndPush) | |
+ (void)load | |
{ | |
@autoreleasepool { | |
for (Class class in RESubclassesOfClass([UIViewController class], YES)) { | |
if ([class conformsToProtocol:@protocol(RLRTableViewCellSelectionDuringInteractivePopAndPush)] | |
&& ![[class superclass] conformsToProtocol:@protocol(RLRTableViewCellSelectionDuringInteractivePopAndPush)] | |
&& ![[class associatedValueForKey:kLoadedKey] boolValue] | |
&& [class instancesRespondToSelector:@selector(tableView)] | |
){ | |
// Raise loaded flag | |
[class setAssociatedValue:@(YES) forKey:kLoadedKey policy:OBJC_ASSOCIATION_RETAIN]; | |
#pragma mark └ -[class viewWillAppear:] | |
RESetBlock(class, @selector(viewWillAppear:), NO, kBlockKey, ^(UIViewController<RLRTableViewCellSelectionDuringInteractivePopAndPush> *vc, BOOL animated) { | |
// supermethod | |
RESupermethod(nil, vc, animated); | |
// Deselect | |
UITableView *tableView; | |
NSIndexPath *indexPath; | |
tableView = vc.tableView; | |
indexPath = [tableView indexPathForSelectedRow]; | |
if (!indexPath) { | |
return; | |
} | |
[tableView deselectRowAtIndexPath:indexPath animated:animated]; | |
#pragma mark └ -[class viewWillDisappear:] | |
RESetBlock(class, @selector(viewWillDisappear:), NO, kBlockKey, ^(UIViewController<RLRTableViewCellSelectionDuringInteractivePopAndPush> *vc, BOOL animated) { | |
// supermethod | |
RESupermethod(nil, vc, animated); | |
// Reselect | |
[tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:UITableViewScrollPositionNone]; | |
}); | |
#pragma mark └ -[class viewDidAppear:] | |
RESetBlock(class, @selector(viewDidAppear:), NO, kBlockKey, ^(UIViewController<RLRTableViewCellSelectionDuringInteractivePopAndPush> *vc, BOOL animated) { | |
// supermethod | |
RESupermethod(nil, vc, animated); | |
// Remove blocks | |
[class removeBlockForInstanceMethod:@selector(viewWillDisappear:) key:kBlockKey]; | |
[class removeBlockForInstanceMethod:@selector(viewDidDisappear:) key:kBlockKey]; | |
RERemoveCurrentBlock(); | |
}); | |
#pragma mark └ -[class viewDidDisappear:] | |
RESetBlock(class, @selector(viewDidDisappear:), NO, kBlockKey, ^(UIViewController<RLRTableViewCellSelectionDuringInteractivePopAndPush> *vc, BOOL animated) { | |
// supermethod | |
RESupermethod(nil, vc, animated); | |
// Remove blocks | |
[class removeBlockForInstanceMethod:@selector(viewWillDisappear:) key:kBlockKey]; | |
[class removeBlockForInstanceMethod:@selector(viewDidAppear:) key:kBlockKey]; | |
RERemoveCurrentBlock(); | |
}); | |
}); | |
} | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment