【问题标题】:Updating Menu Items In UIMenuController更新 UIMenuController 中的菜单项
【发布时间】:2018-10-10 14:58:45
【问题描述】:

我想更新 tableview 菜单项控制器中的菜单项,因为现在我只得到这些

我已经实现了这个:

func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
        let forword = UIMenuItem(title: "Demo", action: #selector(self.demo))
        UIMenuController.shared.menuItems?.append(forword)
        UIMenuController.shared.update()
        return true
    }
    
    func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
        UIMenuController.shared.setMenuVisible(true, animated: true)
    }
    
    func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.clearAllSelectedCell()
    }

但我的要求是这样做:

我怎样才能做到这一点?

【问题讨论】:

  • 是的,我有,但我希望这两个选项更多地出现在菜单控制器中
  • 除了标准项之外,您还想转发和删除,还是代替?
  • 是的,我还想要

标签: ios iphone swift tableview menuitem


【解决方案1】:

您的代码有一些问题……

  • 不要使用UIMenuController.shared.menuItems?.append,只需设置menuItems
  • 不要在shouldShowMenuForRowAt 中执行此操作,只需在viewDidLoad 执行一次即可
  • 你不需要setMenuVisible

最重要的是

  • 菜单项的操作必须是表格视图单元格子类上的方法。

所以,在你的情况下……

你的表格视图控制器应该有这些方法……

override func viewDidLoad() {
    super.viewDidLoad()

    let forward = UIMenuItem(title: "Forward", action: #selector(MyCell.menuItemTapped(_ :)))
    let delete = UIMenuItem(title: "Delete", action: #selector(MyCell.menuItemTapped(_ :)))
    UIMenuController.shared.menuItems = [forward, delete]
    UIMenuController.shared.update()
}

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

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

override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    return true
}

你的细胞应该有……

class MyCell: UITableViewCell {
    @objc func menuItemTapped(_ sender: UIMenuController) {
    }
}

【讨论】:

  • Ashley 您已将其添加到视图中确实加载了。如果我在不同的单元格需要不同的菜单怎么办?
  • 在这种情况下,请在shouldShowMenuForRowAt
  • 我已经做到了,但它没有显示你能帮我吗
  • func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool { let forword = UIMenuItem(title: "Demo", action: #selector(self.demo)) UIMenuController.shared。 menuItems?.append(forword) UIMenuController.shared.update() return true }
  • 请再次阅读我的回答。 “菜单项的操作必须是表格视图单元子类上的方法。”
【解决方案2】:
1.  create protocol to provide control into your view controller :

public protocol MenuItemDelegate {
    func copyAction(cell: UITableViewCell)
    func forwordAction(cell: UITableViewCell)
    func deleteAction(cell: UITableViewCell)
}

2.  create a custom table cell:

        class MyTableCell: UITableViewCell {

            var menuItemDelegate: MenuItemDelegate!

            var isCopyEnable = true
            var isForwardEnable = true
            var isDeleteEnable = true


            override func awakeFromNib() {
                super.awakeFromNib()
            }

            func setUpmenu(){
                let menu = UIMenuController.shared
                let forword = UIMenuItem(title: "Forward", action: #selector(self.forword(_:)))
                let delete = UIMenuItem(title: "Delete", action: #selector(self.deleteAction(_:)))
                menu.menuItems = [forword,delete]
                if !isDeleteEnable{
                    menu.menuItems?.remove(at: (menu.menuItems?.index(of: delete))!)
                }
                if !isForwardEnable{
                    menu.menuItems?.remove(at: (menu.menuItems?.index(of: forword))!)
                }
                menu.update()
            }

            override public func copy(_ sender: Any?) {
                UIPasteboard.general.string = accessibilityValue
                menuItemDelegate.copyAction(cell: self)
                UIMenuController.shared.setMenuVisible(false, animated: true)
            }

            @objc public func forword(_ sender: Any?) {
                menuItemDelegate.forwordAction(cell: self)
                UIMenuController.shared.setMenuVisible(false, animated: true)
            }

            @objc public func deleteAction(_ sender: Any?) {
                menuItemDelegate.deleteAction(cell: self)
                UIMenuController.shared.setMenuVisible(false, animated: true)
            }

            override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
                return (action == #selector(copy(_:))  || action == #selector(forword(_:)) || action == #selector(deleteAction(_:)))
            }
    }

3. Implement in view controller as like : 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableCell", for: indexPath) as! MyTableCell
    cell.isForwardEnable = false
    cell.setUpmenu()
    return cell
}

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

func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
    UIMenuController.shared.setMenuVisible(true, animated: true)
}

func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    return true
}

它将帮助您实现这一目标。

【讨论】:

    【解决方案3】:

    按照以下步骤,实现长按自定义项目。

    1. 将以下代码粘贴到 viewDidload 方法中 let reply = UIMenuItem(title: "Reply", action: #selector(MessageCollectionViewCell.reply(_ :))) let edit = UIMenuItem(title: "Edit", action: #selector(MessageCollectionViewCell.edit(_ :))) let forward = UIMenuItem(title: "Forward", action: #selector(MessageCollectionViewCell.forward(_ :))) UIMenuController.shared.menuItems = [回复、编辑、转发] UIMenuController.shared.update()

    2. 自定义单元格的扩展名 扩展 MessageCollectionViewCell {

      @objc func回复(_发件人:有吗?){ // 获取集合视图 如果让collectionView = self.superview 为? UICollectionView { // 获取索引路径 if let indexPath = collectionView.indexPath(for: self) { // 触发动作 collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.reply(_:)), forItemAt: indexPath, withSender: sender) } } }

      @objc func edit(_ sender: Any?) { // 获取集合视图 如果让collectionView = self.superview 为? UICollectionView { // 获取索引路径 if let indexPath = collectionView.indexPath(for: self) { // 触发动作 collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.edit(_:)), forItemAt: indexPath, withSender: sender) } } }

      @objc func forward(_ sender: Any?) { // 获取集合视图 如果让collectionView = self.superview 为? UICollectionView { // 获取索引路径 if let indexPath = collectionView.indexPath(for: self) { // 触发动作 collectionView.delegate?.collectionView?(collectionView, performAction: #selector(MessageCollectionViewCell.forward(_:)), forItemAt: indexPath, withSender: sender) } } }

    }

    1. 在你的控制器中添加这段代码,在我的例子中是这样做的(类 ChatViewController: MessagesViewController{})

      // --------------------------------------------- ------------------------------------------ // MARK: - 处理长按动作 //------------------------------------------------ -------------------------------------- 覆盖 func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { 返回真 }

      override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { 打印(action.description) 让消息 = 消息 [indexPath.section] //let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)

      if action == NSSelectorFromString("reply:") {
          return true
      }
      else if action == NSSelectorFromString("edit:") {
          return true
      
      }
      else if action == NSSelectorFromString("forward:") {
          return true
      }
      else {
          return super.collectionView(collectionView, canPerformAction: action, forItemAt: indexPath, withSender: sender)
      }
      

      }

      override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {

      if action == NSSelectorFromString("reply:") {
          print("reply item here!!")
      }
      else if action == NSSelectorFromString("edit:") {
          print("edit item here!!")
      }
      else if action == NSSelectorFromString("forward:") {
          print("forward item here!!")
      }
      else {
          super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender)
      }
      

      }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2014-08-23
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      相关资源
      最近更新 更多