How to show a custom UIMenuItem for a UITableViewCell?

As far as I understand there are two main problems:

1) you expect tableView canPerformAction: to support custom selectors while the documentation says it supports only two of UIResponderStandardEditActions (copy and/or paste);

2) there’s no need for the part || action == @selector(test:) as you are adding the custom menu options by initializing menuItems property. For this items selectors the check will be automatical.

What you can do to get the custom menu item displayed and work is:

1) Fix the table view delegate methods with

a)

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
[[UIMenuController sharedMenuController] update];

b)

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return (action == @selector(copy:));
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    // required
}

2) Setup the cells (subclassing UITableViewCell) with

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:) || action == @selector(test:));
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}

/// this methods will be called for the cell menu items
-(void) test: (id) sender {

}

-(void) copy:(id)sender {

}
///////////////////////////////////////////////////////

Leave a Comment