Skip to content

Instantly share code, notes, and snippets.

@jamztang
Created June 28, 2012 08:08
Show Gist options
  • Save jamztang/3009826 to your computer and use it in GitHub Desktop.
Save jamztang/3009826 to your computer and use it in GitHub Desktop.
Fixing the "delete" menu for UITableView shouldShowMenuForRowAtIndexPath
//
// Quick Hack to enable delete menu item
// Original blog post at http://mystcolor.me/post/26114988122/show-delete-menu-item-in-tableview
//
// Updated: 30 June 2012
// I realized there should really be a more elegant way to
// send actions to responder chain, without manually looping
// through the responder chain. Here we implement the
// delete: method in both UITableViewCell and UITableView, so
// that they know the right thing to do in their own delete:
// method
//
@implementation UITableViewCell (DeleteActionResponder)
// We define delete: method so that our UIMenuController delete item shows up
- (void)delete:(id)sender {
NSMutableDictionary *infoDict = [NSMutableDictionary dictionary];
[infoDict setObject:sender
forKey:@"sender"];
[infoDict setObject:self
forKey:@"cell"];
[self.nextResponder delete:infoDict];
}
@end
@implementation UITableView (DeleteActionResponder)
- (void)delete:(id)sender {
NSMutableDictionary *infoDict = sender;
UITableViewCell *cell = [infoDict objectForKey:@"cell"];
id realSender = [infoDict objectForKey:@"sender"];
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self.delegate tableView:self
performAction:@selector(delete:)
forRowAtIndexPath:indexPath
withSender:realSender];
}
@end
@ryanmasondavies
Copy link

Thanks for this. Seems more in line with Apple's 'right way' of implementing UIMenuController on a table than manually adding a long press gesture recognizer.

@jamztang
Copy link
Author

@macrococoa You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment