UIMenuController与UITableViewController不起作用 [英] UIMenuController with UITableViewController doesn't work
问题描述
当用户选择表格中的任何行时,我正在尝试显示 UIMenuController
。我正在使用 UITableViewController
来显示包含自定义单元格的表格。
I am trying to display UIMenuController
when user selects any row in the table. I am using UITableViewController
to display table with custom cell.
我的代码: -
- (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
不没有表现出来。上面的代码有什么问题?
But UIMenuController
doesn't shown. Whats wrong in above code ?
另外,我提到了这些链接。但没有运气。
Also, I've referred these links. But no luck.
推荐答案
如果您只需长按后显示菜单,则无需使用 tableView:didSelectRowAtIndexPath:
并自己显示菜单。
If you're ok with showing the menu only after a long press, there is no need to use tableView:didSelectRowAtIndexPath:
and show the menu yourself.
相反,您可以使用此委托方法:
Instead, you can use this delegate method:
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
要隐藏标准物品(剪切,复制和粘贴),请返回NO这里:
To hide the standard items (cut, copy, and paste), return NO here:
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
然后你需要返回YES
来自 canBecomeFirstResponder
就像你一样,由于某种原因,我也必须实现这个方法:
Then you need to return YES
from canBecomeFirstResponder
like you have and, for some reason, I had to implement this method too:
- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return YES;
}
这篇关于UIMenuController与UITableViewController不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!