Skip to content

Instantly share code, notes, and snippets.

@stefanceriu
Last active November 20, 2021 04:13
Show Gist options
  • Save stefanceriu/eb7730eadc37894deb9c7b96c0e85168 to your computer and use it in GitHub Desktop.
Save stefanceriu/eb7730eadc37894deb9c7b96c0e85168 to your computer and use it in GitHub Desktop.
Enabled iOS style UITableView multiple selection on Catalyst applications
//
// UITableView+CTX.m
// EFClass-Mac
//
// Created by Stefan Ceriu on 29/11/2019.
// Copyright © 2019 EF Education First. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UITableView (CTX)
@end
//
// UITableView+CTX.m
// EFClass-Mac
//
// Created by Stefan Ceriu on 29/11/2019.
// Copyright © 2019 EF Education First. All rights reserved.
//
#import "UITableView+CTX.h"
#import <objc/runtime.h>
@implementation UITableView (CTX)
+ (void)load
{
#if TARGET_OS_MACCATALYST
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CTXSwizzleInstanceMethod([self class], NSSelectorFromString(@"_selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:isCellMultiSelect:"),
@selector(ctx_selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:isCellMultiSelect:));
CTXSwizzleInstanceMethod([self class], NSSelectorFromString(@"_deselectRowAtIndexPath:animated:notifyDelegate:"),
@selector(ctx_deselectRowAtIndexPath:animated:notifyDelegate:));
});
#endif
}
- (void)ctx_selectRowAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UITableViewScrollPosition)scrollPosition
notifyDelegate:(BOOL)notifyDelegate
isCellMultiSelect:(BOOL)isCellMultiSelect
{
if(!self.allowsMultipleSelection) {
return [self ctx_selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition notifyDelegate:notifyDelegate isCellMultiSelect:isCellMultiSelect];
}
BOOL selected = [self.indexPathsForSelectedRows containsObject:indexPath];
if(selected) {
[self ctx_deselectRowAtIndexPath:indexPath animated:animated notifyDelegate:notifyDelegate];
} else {
[self ctx_selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition notifyDelegate:notifyDelegate isCellMultiSelect:YES];
}
}
- (void)ctx_deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated notifyDelegate:(BOOL)notifyDelegate
{
return [self ctx_deselectRowAtIndexPath:indexPath animated:animated notifyDelegate:notifyDelegate];
}
static void CTXSwizzleInstanceMethod(Class class, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
if (class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment