Reorder Rows from Table View iOS Tutorial

The Table View Controller has an editing mode, when enabled the rows can be reordered by dragging the cells up/down. In this tutorial we will fill the Table View with some data and put the Table View Controller in editing mode. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use iOS12ReorderRowsTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard and Remove the View Controller. Drag a Navigation Controller from the Object Library to the empty canvas, this will also contain a table view controller. When the initial View Controller is deleted there isn't a starting point defined. Select the Navigation Controller and go to the Attribute Inspector. In the View Controller Section  select the "Is Initial View Controller" checkbox.

is-initial-view-controller.png

Double-click the Title Bar in the Table View Controller and insert the title Numbers. The storyboard should look like this.

We will use a basic layout of our cells, so select the Table View Cell and go to the Attributes Inspector. Change the style to Basic and insert as Identifier numberCell. We will need this Identifier as a reference into our code.

Go to the ViewController.swift file. Since we have changed the default View Controller into an Table View Controller we also need to update the class declaration line in

class ViewController: UITableViewController {

Next, we create an array containing the numbers. 

var numbers = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"]

We also need to determine the number of rows in our Table View. Add the tableView(_:numberOfRowsInSection:) method

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numbers.count
}

We will need to fill our cells with data. Add the tableView(_:cellForRowAt:) method

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "numberCell", for: indexPath)
    cell.textLabel?.text = numbers[indexPath.row]
            
    return cell
}

Build and Run the project, The rows of the Table Views are filled with the numbers from the array.

To reorder the rows in a Table View we have to make sure our Table View is in editing mode. Go to the Main.Storyboard and add a Bar Button Item to the right of the Navigation Bar of the Table View Controller. Select the Bar button item and in the Attributes inspector change the title to Edit.

Open The Assistant Editor and make sure the TableViewController.swift file is visible. Ctrl And Drag from the Bar Button Item  to the TableViewController class and create the following Action

Implement the startEditing method

@IBAction func startEditing(_ sender: Any) {
    isEditing = !isEditing
}

The Table View Controller has an editing property to set the Table View into editing mode.  While in editing mode the table view rows can be reordered. Pressing there Edit button will toggle the editing mode. The tableView has 2 delegate methods which are needed to control the movement of our rows. First create the following method so all rows can be moved

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

In the tableView(_:moveRowAt:to:) we will reorder the items in the numbers array.

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let itemToMove = numbers[sourceIndexPath.row]
    numbers.remove(at: sourceIndexPath.row)
    numbers.insert(itemToMove, at: destinationIndexPath.row)
}

Build and Run and reorder the rows in our table view.

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