How to show a custom UIMenuItem for a UITableViewCell?

后端 未结 3 627
名媛妹妹
名媛妹妹 2020-11-29 01:56

I want the UIMenuController that pops up when I long-press a UITableViewCell to show custom UIMenuItems.

I set up the custom item in viewDidLoad

UIMe         


        
3条回答
  •  日久生厌
    2020-11-29 02:41

    SWIFT 3:

    In AppDelegate didFinishLaunchingWithOptions:

    let customMenuItem = UIMenuItem(title: "Delete", action:
    #selector(TableViewCell.deleteMessageActionTapped(sender:)))
            UIMenuController.shared.menuItems = [customMenuItem]
            UIMenuController.shared.update()
    

    in your TableViewContoller Class:

    override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
            return action == #selector(copy(_:)) || action == #selector(TableViewCell.yourActionTapped(sender:))
        }
    
    
    
     override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
       if action == #selector(copy(_:)) {
            let pasteboard = UIPasteboard.general
            pasteboard.string = messages[indexPath.row].text
       }
    }
    

提交回复
热议问题