/
FEXTappableConfirmationButton.m
48 lines (40 loc) · 2.24 KB
/
FEXTappableConfirmationButton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#import <ObjC/runtime.h>
#import "FEXTappableConfirmationButton.h"
#import "UITableViewCell+TappableConfirmationButton.h"
void FEX_confirmDeletion(id self, SEL _cmd) {
UITableViewCell *cell = (UITableViewCell *)[(UIView *) self superview];
UITableView *tableView = (UITableView *)[cell superview];
id <UITableViewDataSource> dataSource = [tableView dataSource];
// If the data source does not implement tableView:commitEditingStyle:forRowAtIndexPath:,
// the table is unable to delete cells. See the diagram and discussion here:
// http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html
if (![dataSource respondsToSelector:@selector(tableView:commitEditingStyle:forRowAtIndexPath:)]) return;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
[dataSource tableView:tableView
commitEditingStyle:UITableViewCellEditingStyleDelete
forRowAtIndexPath:indexPath];
// If the cell is showing a delete button, then the
// user entered edit mode by tapping the table's Edit
// button. Otherwise, the user entered edit mode by
// swiping the cell. If the user entered edit mode by
// swiping, we need to end edit mode after confirming
// deletion. If we entered edit mode by tapping the
// Edit button, we will leave it to the user to end
// edit mode.
BOOL shouldEndEditing = ![cell FEX_isShowingDeleteButton];
if (shouldEndEditing) {
[tableView setEditing:NO animated:YES];
id <UITableViewDelegate> delegate = tableView.delegate;
if ([delegate respondsToSelector:@selector(tableView:didEndEditingRowAtIndexPath:)]) {
[delegate tableView:tableView didEndEditingRowAtIndexPath:indexPath];
}
}
}
@implementation FEXTappableConfirmationButton
+ (void)install {
Class confirmationButtonClass = NSClassFromString(@"UITableViewCellDeleteConfirmationControl");
char *const voidNoArgsType = "v@:";
class_replaceMethod(confirmationButtonClass, NSSelectorFromString(@"tap"), (IMP) FEX_confirmDeletion, voidNoArgsType);
class_replaceMethod(confirmationButtonClass, NSSelectorFromString(@"touch"), (IMP) FEX_confirmDeletion, voidNoArgsType);
}
@end