如何在不影响消息 UIMenuItem 的情况下禁用默认 UIMenuItem?

How to disable the default UIMenuItem without affecting messages UIMenuItem?

有一种方法可以禁用默认的 UIMenuItem

不影响消息 UIMenuItem?

ViewDidLoad:

JSQMessagesCollectionViewCell.registerMenuAction(#selector(UIResponderStandardEditActions.delete(_:)))
    UIMenuController.shared.menuItems = [UIMenuItem.init(title: "Delete", action: Selector(("delete")))]

使用

cell.textView.selectable = falsecellForRow 方法中

在这里您可以选择长按消息气泡时出现的内容。

override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {

    if (action == #selector(UIResponderStandardEditActions.copy(_:))) {

        if(messages[indexPath.row].isMediaMessage) {
            return false
        } else {
            return true
        }
    }
    if (action == #selector(UIResponderStandardEditActions.cut(_:))) {

        if(messages[indexPath.row].isMediaMessage) {
            return false
        } else {
            return false
        }
    }
    if (action == #selector(UIResponderStandardEditActions.paste(_:))) {

        if(messages[indexPath.row].isMediaMessage) {
            return false
        } else {
            return false
        }
    }

    if (action == #selector(UIResponderStandardEditActions.delete(_:))) {

        if(messages[indexPath.row].isMediaMessage) {
            return true
        } else {
            return true
        }
    }

    return super.canPerformAction(action, withSender: sender)
}

然后select你想要的项目之后会发生什么,

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

    if (action == #selector(UIResponderStandardEditActions.delete(_:))) {
        print("deleted")
        print(indexPath.row)

        let messageKey = messages[indexPath.row].keyID

        print("messageKey")
        print(messageKey!)

        messages.remove(at: indexPath.row)
        collectionView.reloadData()
    }
    if (action == #selector(UIResponderStandardEditActions.copy(_:))) {
        // do stuff
    }
}

}