1

我试图UIMenuController在用户选择表中的任何行时显示。我UITableViewController用来显示带有自定义单元格的表格。

我的代码:-

- (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;
}

UIMenuController没有显示。上面的代码有什么问题?

另外,我参考了这些链接。但没有运气。

4

1 回答 1

0

如果您只在长按后才显示菜单没问题,则无需自己使用tableView:didSelectRowAtIndexPath:和显示菜单。

相反,您可以使用此委托方法:

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

要隐藏标准项目(剪切、复制和粘贴),请在此处返回 NO:

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

然后你需要return YEScanBecomeFirstResponder你一样,出于某种原因,我也必须实现这个方法:

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}
于 2013-02-03T09:23:59.193 回答