Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show "Copy" menu for a table cell?

I would add the option to copy the selected cell in a table like in the contacts app.

copy menu

I have tried to follow this question about Objective-C and impliment those 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, that is an old question, and I can't get the menu to show up using Swift code. Could someone explain how the shouldShowMenuForRowAtIndexPath method is used and how to allow for a user to copy a cell.

like image 249
54 69 6D Avatar asked Mar 26 '16 22:03

54 69 6D


1 Answers

You refer to an Objective-C example, but you have not done what it says to do! Your second method is the wrong method. You want to say this:

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

You will also need a third override:

override func tableView(tableView: UITableView, performAction action: Selector, 
    forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
        // ...
}
like image 154
matt Avatar answered Oct 23 '22 23:10

matt