本文介绍了在UITableView中点击单元格时显示UIMenuController时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

在cellForRowAtIndexPath中我附上我的长按手势识别器

In the cellForRowAtIndexPath I attach my 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];

这是我的handleLongPress操作方法

Here is my handleLongPress action method

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

编辑和保存的操作方法只显示一个UIAlertView中。我还实现了以下方法,以确保在显示UIMenuController时,仅存在保存和编辑选项

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

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

 BOOL canPerform = NO;

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

 return canPerform;
}

我还声称MyViewController是第一响应者

I'm also claiming MyViewController to be first responder

-(BOOL)canBecomeFirstResponder {

  return YES;
}


推荐答案

我相信你需要拥有声明firstResponder状态以显示UIMenuController的视图。我没有在你的代码中看到这种情况。

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.

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

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

这篇关于在UITableView中点击单元格时显示UIMenuController时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 12:14