ユーザーがグループ化されたUITableViewのセルを長押ししたときに、カスタムUIMenuControllerを表示しようとしています。ただし、長押しを正常に検出した後、UIMenuControllerを表示できないようです。どんな助けでも大歓迎です。
MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
UITableView *table;
@property (nonatomic, retain) IBOutlet UITableView *table;
@end
cellForRowAtIndexPathに、Long PressGestureRecognizerを接続します
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionsTableIdentifier] autorelease];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[cell addGestureRecognizer:longPress];
[longPress release];
これが私の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];
}
}
EditおよびSaveの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;
}
私はまた、MyViewControllerがファーストレスポンダーであると主張しています
-(BOOL)canBecomeFirstResponder {
return YES;
}