如何为 UITableViewCell 显示自定义 UIMenuItem? [英] How to show a custom UIMenuItem for a UITableViewCell?
问题描述
我希望在我长按 UITableViewCell 时弹出的 UIMenuController 显示自定义 UIMenuItems.
I want the UIMenuController that pops up when I long-press a UITableViewCell to show custom UIMenuItems.
我在 viewDidLoad 中设置了自定义项
I set up the custom item in viewDidLoad
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
然后我设置了所有正确的委托方法.
And then I set all the right delegate methods.
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return (action == @selector(copy:) || action == @selector(test:));
}
- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
// do stuff
}
return YES;
}
但它所做的只是显示复制"项目,因为我只允许它和我的自定义项目.但是,自定义项目不会显示.
But all it does, is show the "Copy" item, since I only allow it and my custom item. The custom item, however, won't show up.
我意识到,我可以向单元格本身添加一个手势识别器,但这违背了 UIMenuController 共享实例的目的,不是吗?
I realize, I could add a gesture recognizer to the cell itself, but that kind of defeats the purpose of the shared instance of UIMenuController, doesn't it?
推荐答案
据我了解主要有两个问题:
As far as I understand there are two main problems:
1) 您希望 tableView canPerformAction:
支持自定义选择器,而文档说它只支持两个 UIResponderStandardEditActions
(复制和/或粘贴);
1) you expect tableView canPerformAction:
to support custom selectors while the documentation says it supports only two of UIResponderStandardEditActions
(copy and/or paste);
2) 不需要 ||action == @selector(test:)
当您通过初始化 menuItems
属性添加自定义菜单选项时.对于这个项目选择器,检查将是自动的.
2) there's no need for the part || action == @selector(test:)
as you are adding the custom menu options by initializing menuItems
property. For this items selectors the check will be automatical.
您可以做些什么来显示和工作的自定义菜单项:
What you can do to get the custom menu item displayed and work is:
1) 使用
a)
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
[[UIMenuController sharedMenuController] update];
b)
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return (action == @selector(copy:));
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
// required
}
2) 使用
-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
return (action == @selector(copy:) || action == @selector(test:));
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
/// this methods will be called for the cell menu items
-(void) test: (id) sender {
}
-(void) copy:(id)sender {
}
///////////////////////////////////////////////////////
这篇关于如何为 UITableViewCell 显示自定义 UIMenuItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!