在UITableView中点击单元格时显示UIMenuController时出现问题

时间:2022-11-21 21:21:08

I'm trying to display a custom UIMenuController when a User long presses on a cell in a grouped UITableView. However, I can't seem to get the UIMenuController to display after successfully detecting the long press. Any help is greatly appreciated.

当用户长时间按下分组UITableView中的单元格时,我正在尝试显示自定义UIMenuController。但是,在成功检测到长按后,我似乎无法显示UIMenuController。任何帮助是极大的赞赏。

MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
  UITableView *table;
  @property (nonatomic, retain) IBOutlet UITableView *table;
@end

In the cellForRowAtIndexPath I attach my Long Press Gesture Recognizer

在cellForRowAtIndexPath中,我附上了Long Press Gesture Recognizer

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionsTableIdentifier] autorelease];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [cell addGestureRecognizer:longPress];
    [longPress release];

Here is my handleLongPress action method

这是我的handleLongPress动作方法

-(void)handleLongPress:(UIGestureRecognizer *)longPress {

  if (longPress.state == UIGestureRecognizerStateBegan) {

    CGPoint pressLocation = [longPress locationInView:self.table];
    NSIndexPath *pressedIndexPath = [self.table indexPathForRowAtPoint:pressLocation];

    UIMenuItem *first = [[UIMenuItem alloc] initWithTitle:@"Save" action:@selector(saveRecent)];
    UIMenuItem *second = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editQuery)];

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = [NSArray arrayWithObjects:first,second,nil];

    [menuController setTargetRect:longPress.view.frame inView:longPress.view.superview];
    [menuController setMenuVisible:YES animated:YES];
    [pressedIndexPath release];
  }
}

The Action methods for the Edit and Save just display a UIAlertView. I also implemented the below method to ensure that when the UIMenuController is displayed only Save and Edit options will be present

编辑和保存的Action方法只显示UIAlertView。我还实现了以下方法,以确保在显示UIMenuController时,仅存在“保存”和“编辑”选项

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {

 BOOL canPerform = NO;

 if (action == @selector(saveRecent)) {
    canPerform = YES;
 }
 if (action == @selector(editQuery)) {
    canPerform = YES;
 }

 return canPerform;
}

I'm also claiming MyViewController to be first responder

我也声称MyViewController是第一响应者

-(BOOL)canBecomeFirstResponder {

  return YES;
}

1 个解决方案

#1


1  

I believe you need to have a view claiming firstResponder status in order to present the UIMenuController. I don't see that happening in your code.

我相信你需要有一个视图声明firstResponder状态才能呈现UIMenuController。我没有看到你的代码中发生了这种情况。

I wrote up directions for using the UIMenuController as an answer to this question:

我写了使用UIMenuController作为这个问题的答案的指示:

Customize UIMenuController

#1


1  

I believe you need to have a view claiming firstResponder status in order to present the UIMenuController. I don't see that happening in your code.

我相信你需要有一个视图声明firstResponder状态才能呈现UIMenuController。我没有看到你的代码中发生了这种情况。

I wrote up directions for using the UIMenuController as an answer to this question:

我写了使用UIMenuController作为这个问题的答案的指示:

Customize UIMenuController