更新UIMenuController中的菜单项

时间:2018-10-10 14:58:46

标签: ios iphone swift tableview menuitem

我想更新tableview菜单项控制器中的菜单项,因为现在我只能获取这些菜单项 enter image description here

我已经实现了:

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()
    }

但是我的要求是做到这一点:

enter image description here

我该如何实现?

3 个答案:

答案 0 :(得分: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) {
    }
}

答案 1 :(得分:1)

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
}

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

答案 2 :(得分:0)

请按照以下步骤操作,长按即可获得自定义项目。

  1. 将以下代码粘贴到viewDidload方法中 让reply = UIMenuItem(标题:“ Reply”,操作:#selector(MessageCollectionViewCell.reply(_ :))) 让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     如果让collectionView = self.superview为? UICollectionView {         //获取indexPath         如果让indexPath = collectionView.indexPath(for:self){             //触发动作             collectionView.delegate?.collectionView?(collectionView,performAction:#selector(MessageCollectionViewCell.reply(_ :)),forItemAt:indexPath,withSender:sender)         }     } }

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

    @objc func forward(_发件人:可以吗?){     //获取collectionView     如果让collectionView = self.superview为? UICollectionView {         //获取indexPath         如果让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 {     返回真 }

    重写func collectionView(_ collectionView:UICollectionView,canPerformAction操作:选择器,forItemAt indexPath:IndexPath,withSender发送者:Any?)-> Bool {     打印(action.description)     让message = messages [indexPath.section]     //让message = messagesDataSource.messageForItem(位于:indexPath,位于: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)
    }
    

    }

    重写func collectionView(_ collectionView:UICollectionView,performAction操作:选择器,forItemAt indexPath:IndexPath,withSender发送者:任何?){

    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)
    }
    

    }