如何为UITableViewCell显示自定义UIMenuItem? [英] How to show a custom UIMenuItem for a UITableViewCell?

查看:79
本文介绍了如何为UITableViewCell显示自定义UIMenuItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当我长按UITableViewCell以显示自定义UIMenuItems时弹出UIMenuController。

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)修复表格视图委托方法

1) Fix the table view delegate methods with

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)设置单元格(子类化 UITableViewCell

2) Setup the cells (subclassing UITableViewCell) with

-(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆