Search code examples
uitableviewuimenucontroller

show UIMenuController in UITableViewCell, grouped style


Is there a simple way to implement the copy menu when a cell is tapped, instead of subclassing the UITableViewCell?

thanks,

RL


Solution

  • Yes!
    Call [[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani] (where ani is a BOOL determining whether the controller should be animated) from within - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ( UITableView's delegate method)

    Edit: The 'copy' command on the UIMenuController will not by default copy the detailTextLabel.text text. However, there is a workaround. Add the following code into your class.

    -(void)copy:(id)sender {
        [[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
    }
    
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if(action == @selector(copy:)) {
            return YES;
        }
        else {
            return [super canPerformAction:action withSender:sender];
        }
    }