How to show the Copy menu for a table cell?

I would add the option to copy the selected cell to the table, as in the contacts application.

copy menu

I tried to fulfill this question about Objective-C and implement these methods in Swift:

override func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    return (action == #selector(NSObject.copy(_:)))
}

However, this is an old question, and I cannot get the menu to appear using Swift code. Can anyone explain how the method is used shouldShowMenuForRowAtIndexPathand how to allow the user to copy the cell.

+4
source share
1 answer

You refer to the Objective-C example , but you did not do what it said! The second method is the wrong method. You want to say the following:

override func tableView(tableView: UITableView, canPerformAction action: Selector, 
    forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) 
    -> Bool {
        return action == #selector(copy(_:))
}

:

override func tableView(tableView: UITableView, performAction action: Selector, 
    forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
        // ...
}
+8

All Articles