(关闭)Swift-UICollectionView关联菜单行

(关闭)Swift-UICollectionView关联菜单行,swift,uicollectionview,Swift,Uicollectionview,我目前正在实现一个UICollectionView以在我的应用程序中显示CoreData数据,我想使用ContextMenu为每个单元格添加delete函数 在internet上,我观察到许多页面可以在UITableView和UICollectionView的单元格中自定义UIMenuController,但我仍然无法显示我的MenuItem delete 这是我的代码: private func config_menu() { let menuItem = UIMenuItem(tit

我目前正在实现一个UICollectionView以在我的应用程序中显示CoreData数据,我想使用ContextMenu为每个单元格添加delete函数

在internet上,我观察到许多页面可以在UITableView和UICollectionView的单元格中自定义UIMenuController,但我仍然无法显示我的MenuItem delete

这是我的代码:

private func config_menu()
{
    let menuItem = UIMenuItem(title: "Test", action: #selector(test(_:)))
    let menu = UIMenuController.shared
    menu.menuItems = [menuItem]
    menu.update()
}

@objc func test(_ sender: Any?)
{

}
集合视图:

    func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
        return true
    }

    func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        if (action == #selector(copy(_:)) || action == #selector(test(_:)))
        {
            return true
        }
        return false
   }

   func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
       print("Hello world")
   }

怎么了?

我找到了解决方案:D要能够使用自定义菜单项,有必要在单元格的子类中声明它们,方法如下:

class Cell_Prueba: UICollectionViewCell {


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

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

    @objc func test(_ sender: Any?)
    {
        //Do Any
    }
}