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 YESする必要がありました:canBecomeFirstResponder

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