UIMenuController with UITableViewController doesn't work

大城市里の小女人 提交于 2020-01-06 01:55:10

问题


I am trying to display UIMenuController when user selects any row in the table. I am using UITableViewController to display table with custom cell.

my code:-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // [tableView deselectRowAtIndexPath:indexPath animated:NO];

    MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];
    CGRect cellFrame = cell.frame;

    [self.view becomeFirstResponder];

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Item1" action:@selector(action1:)];
    UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Item2" action:@selector(action2:)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Item3" action:@selector(action3:)];

    UIMenuController * menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = [NSArray arrayWithObjects:menuItem, menuItem1, menuItem2, nil];
    menuController.arrowDirection = UIMenuControllerArrowDown;

    [menuController setTargetRect:cellFrame inView:self.view];

    [menuController setMenuVisible:YES animated:YES];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

But UIMenuController doesn't shown. Whats wrong in above code ?

Also, I've referred these links. But no luck.


回答1:


If you're ok with showing the menu only after a long press, there is no need to use tableView:didSelectRowAtIndexPath: and show the menu yourself.

Instead, you can use this delegate method:

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

To hide the standard items (cut, copy, and paste), return NO here:

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

Then you need to return YES from canBecomeFirstResponder like you have and, for some reason, I had to implement this method too:

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}


来源:https://stackoverflow.com/questions/14473959/uimenucontroller-with-uitableviewcontroller-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!