首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示UITableViewCell的自定义UIMenuItem?

如何显示UITableViewCell的自定义UIMenuItem?
EN

Stack Overflow用户
提问于 2012-09-06 06:47:14
回答 4查看 22.8K关注 0票数 24

我想要当我长按UITableViewCell以显示自定义UIMenuItems时弹出的UIMenuController。

我在viewDidLoad中设置了自定义项目

代码语言:javascript
复制
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];

然后我设置了所有正确的委托方法。

代码语言:javascript
复制
- (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;
}

但它所做的一切,就是显示“复制”项,因为我只允许它和我的自定义项。但是,自定义项不会显示。

我意识到,我可以向单元格本身添加一个手势识别器,但这有点违背了UIMenuController共享实例的目的,不是吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-09-06 07:44:59

据我了解,主要有两个问题:

1)你期望tableView canPerformAction:支持自定义选择器,而文档说它只支持两个UIResponderStandardEditActions (复制和/或粘贴);

2)由于您通过初始化menuItems属性来添加自定义菜单选项,因此不需要part || action == @selector(test:)。对于此项目选择器,检查将是自动的。

您可以执行以下操作来显示自定义菜单项:

1)修复表视图委托方法

a)

代码语言:javascript
复制
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[[UIMenuController sharedMenuController] setMenuItems: @[testMenuItem]];
[[UIMenuController sharedMenuController] update];

b)

代码语言:javascript
复制
- (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)

代码语言:javascript
复制
-(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 {

}
///////////////////////////////////////////////////////
票数 52
EN

Stack Overflow用户

发布于 2015-07-11 22:59:50

为UITableViewCell实现复制和自定义操作的

在应用程序中注册后,注册自定义操作:

代码语言:javascript
复制
struct Token { static var token: dispatch_once_t = 0 }
dispatch_once(&Token.token) {
    let customMenuItem = UIMenuItem(title: "Custom", action: #selector(MyCell.customMenuItemTapped(_:))
    UIMenuController.sharedMenuController().menuItems = [customMenuItem]
    UIMenuController.sharedMenuController().update()
}

在您的UITableViewCell子类中,实现自定义方法:

代码语言:javascript
复制
func customMenuItemTapped(sender: UIMenuController) {
    // implement custom action here
}

在您的UITableViewDelegate,中,实现以下方法:

代码语言:javascript
复制
override func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
    return action == #selector(NSObject.copy(_:)) || action == #selector(MyCell.customMenuItemTapped(_:))
}

override func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    switch action {
    case #selector(NSObject.copy(_:)):
        // implement copy here
    default:
        assertionFailure()
    }
}

备注:

对于how to implement copy.上的信息,此答案使用新的Swift 3 #selectors.

  • See
票数 8
EN

Stack Overflow用户

发布于 2021-01-26 00:51:08

仅允许复制节0的第0行的示例

已更新至Swift 5.2

代码语言:javascript
复制
func shouldAllowCopyOn(indexPath: IndexPath) -> Bool {
    if indexPath.section == 0 && indexPath.row == 0 {
       return true
    }
    return false
}

func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
    return self.shouldAllowCopyOn(indexPath: indexPath)
}

func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    if (action == #selector(UIResponderStandardEditActions.copy(_:))) {
          return self.shouldAllowCopyOn(indexPath: indexPath)
    }
}

func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
    if (action == #selector(UIResponderStandardEditActions.copy(_:)) && self.shouldAllowCopyOn(indexPath: indexPath)) {
       if let cell = self.tableView.cellForRow(at: indexPath) as? UITableViewCell {
         self.copyAction(cell: cell)
        }
    }
}

@objc
private func copyAction(cell: UITableViewCell) {
    UIPasteboard.general.string = cell.titleLabel.text
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12290828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档