首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表中未显示UIMenuItem

表中未显示UIMenuItem
EN

Stack Overflow用户
提问于 2018-08-06 11:33:12
回答 1查看 174关注 0票数 0

我试图在UIMenuController中添加一个自定义操作,以便在UITableViewCell上使用,并且它在显示菜单时不会出现。

编辑:修改代码。

以下是代码:

代码语言:javascript
复制
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    override func viewDidLoad() {
        ...
        UIMenuController.shared.menuItems = [UIMenuItem(title: "Test", action: #selector(test))]
        UIMenuController.shared.update()
    }

    // Table view setup
    // ...
    func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return action == #selector(copy(_:)) || action == #selector(test)
    }
    func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return action == #selector(test)
    }

    @objc func test() {
        print("Hello, world!")
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 11:47:16

  1. 您的test函数需要在UITableViewCell子类中。
  2. 您需要在那个canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool子类中实现UITableViewCell并返回return action == #selector(test)
  3. UIMenuController.shared.menuItems = [UIMenuItem(title: "Test", action: #selector(test))]中,将#selector(test)更改为#selector(YourCellSubclass.test)
  4. 保留视图控制器中的UITableViewDelegate方法,并将|| action == #selector(test)更改为|| action == #selector(YourCellSubclass.test)

编辑:添加工作示例。

ViewController:

代码语言:javascript
复制
class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIMenuController.shared.menuItems = [UIMenuItem(title: "Test", action: #selector(MyCell.test))]
        UIMenuController.shared.update()

        tableView.register(MyCell.self, forCellReuseIdentifier: "my")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath) as! MyCell
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }

    override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return action == #selector(MyCell.test)
    }

    override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
        // needs to be here
    }

}

牢房:

代码语言:javascript
复制
class MyCell: UITableViewCell {

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return action == #selector(test)
    }

    @objc func test() {
        print("works")
    }

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51706678

复制
相关文章

相似问题

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