Use Context Menu with Table View Tutorial

A long-press gesture displays a Context Menu, which gives the user the ability to use cut/copy/paste operations on the selected object. By default, the Context Menu is disabled on a Table View. In this tutorial the context menu will be enabled to copy the text of the Table View Cell, this text can then be pasted into a Text Field. This tutorial is made with Xcode 7 Beta 5 and built for iOS 9.

Open Xcode and create a new Single View Application. For product name, use IO9ContextMenuTableViewTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Open The Main.storyboard file and drag a Table View from the Object Library to the top of the main View.  Select the Table View, go to the Attribute Inspector and in the Table View section change the Prototype Cells field to a value of 1

Select the Table View Cell and go to the Attribute Inspector. In the Table View Cell section set the Identifier to "cell".

Select the Table View, select the Pin button on the bottom-right of the storyboard and pin the Table View on the top, left and right. Also select the Height Attribute to give the Table View a fixed height. Next, select Add 4 Constraints.

Drag a Text Field from the Object Library and place it right below the Table View. Ctrl and drag from inside the Text Field to the Table View. Hold Down Ctrl and select "Vertical Spacing" and "Center Horizontally".

Select the Text Field, select the Pin button on the bottom-right of the storyboard and pin the Text Field  to the left and right.

The View Controller needs to be the delegate of the Table View. Select the TableView, Ctrl and Drag to the View Controller icon at the top of the main View. Select Datasource. Repeat this step and select delegate.

Repeat this also for the Text Field to make the View Controller the delegate for it. Go to the ViewController.swift file and change the class declaration line tp

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

Declare the following properties.

var pasteBoard = UIPasteboard.generalPasteboard()
var tableData: [String] = ["dog","cat","fish"]

The pasteBoard property will be used for copy-paste operations and the tableData holds the contents which will be displayed on the Table View Cells. Next, declare the Table View delegate methods.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableData.count
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = tableData[indexPath.row]
        return cell

The Table View will be filled with the three values from the TableData array. To enable the Context  Menu the following three delegate methods must be implemented.

func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
        if (action == Selector("copy:")) {
            return true
        return false
    func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
            let cell = tableView.cellForRowAtIndexPath(indexPath)
            pasteBoard.string = cell!.textLabel?.text

The tableView:shouldShowMenuForRowAtIndexPath method must return true to display the Context Menu when a Table View Cell is long-pressed. In the tableView:canPerformAction:forRowAtIndexPath method only the copy item is displayed. The tableView:performAction:forRowAtIndexPath:withSender method copies the selected text into the pasteBoard. 

Finally, implement the textFieldShouldReturn method to dismiss the keyboard when pressing enter when in the editing mode in the Text Field.

func textFieldShouldReturn(textField: UITextField) -> Bool {
        return false

Build and Run the project, Long-press a Table View Cell and Select the copy item. Paste the text inside the Text Field.

You can download the source code of the IO9ContextMenuTableViewTutorial at the ioscreator repository on Github.