2

除了WWDC 视频 - 44:18一个示例项目,网上没有太多信息。

所以actionProvider,当被调用时,有一个suggestedActions 系统传递给它的列表。

这可以是UIMenus 和UIActions 的混合,因此可能是从系统中提取的完全构造的层次结构。这些可能是您使用 iOS 13 中引入的新 UI 命令 API 在响应者链中定义的内容,也可能是其他系统组件提供的内容。所以我们在这里做一个完全自定义的菜单,所以我们将把它放在suggestedActions一边。首先,我们将创建我们的编辑菜单。

但到目前为止,我了解到的是,给定的标识符可以具有预定义的建议操作,suggestionActions这些操作不与任何标识符相关联。如果我更改该标识符,suggestedActions则不会更改。它似乎是基于它的上下文,在这里我猜我在 tableviewcell 的上下文中......

我试图看看它是什么样子,但什么也没有出现。如果我点击单元格,则没有任何显示。

class ViewController: UIViewController {
    let tableview: UITableView = {
        let tv = UITableView()
        tv.frame = UIScreen.main.bounds

        return tv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableview)
        tableview.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if cell.detailTextLabel == nil {
            cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        }
        cell.textLabel?.text = "Honey"
        cell.detailTextLabel?.text = "iOS developer"

        return cell
    }

    @available(iOS 13.0, *)
    func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {

        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
            print("suggestedActions", suggestedActions)
            let menu = UIMenu(title: "", children: suggestedActions)

            return menu
        })
    }
}

我也打印suggestedActions了,我得到的是:

  - 0 : <UIMenu: 0x6000008e6790; title = Standard Edit; identifier = com.apple.menu.standard-edit; options = (Inline); children = <NSArray: 0x600001318900>>
  - 1 : <UIMenu: 0x6000008e6820; title = Replace; identifier = com.apple.menu.replace; options = (Inline); children = <NSArray: 0x6000008e64c0>>
  - 2 : <UIMenu: 0x6000008e7330; title = Text Style; identifier = com.apple.menu.text-style; image = <UIImage:0x60000349c870 symbol(system: bold.italic.underline) {36, 17} baseline=3.66667,contentInsets={1, 2, 1, 2},alignmentRectInsets={-2.9999999999999982, 0, -0.33333333333333348, 0} config=<(null), traits=(UserInterfaceIdiom = Phone, DisplayScale = 3, DisplayGamut = P3, HorizontalSizeClass = Compact, VerticalSizeClass = Regular, UserInterfaceStyle = Light, UserInterfaceLayoutDirection = LTR, PreferredContentSizeCategory = L)>>; children = <NSArray: 0x6000008e6df0>>
  - 3 : <UIMenu: 0x6000008e6d00; title = Lookup; identifier = com.apple.menu.lookup; options = (Inline); children = <NSArray: 0x60000049c870>>
  - 4 : <UIMenu: 0x6000008e7060; title = Learn; identifier = com.apple.menu.learn; options = (Inline); children = <NSArray: 0x60000049c220>>
  - 5 : <UIMenu: 0x6000008e6d90; title = Speech; identifier = com.apple.command.speech; options = (Inline); children = <NSArray: 0x6000008e6490>>
  - 6 : <UIMenu: 0x6000008e7180; title = Share; identifier = com.apple.menu.share; options = (Inline); children = <NSArray: 0x60000049c6a0>>
  - 7 : <UIMenu: 0x6000008e6a30; title = Writing Direction; identifier = com.apple.menu.writing-direction; options = (Inline); children = <NSArray: 0x6000006fe1c0>>
4

0 回答 0