jamztang
6/28/2012 - 8:08 AM

Fixing the "delete" menu for UITableView shouldShowMenuForRowAtIndexPath

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