Dragging and dropping to reorder cells

Now, let's implement the reordering of cells:

  1. To enable the reordering of cells, you need exactly three steps. The first step is to enable the showing of the reordering control in cells. To do this, just do the following in the cellForRow method:
      cell?.showsReorderControl = true 
  1. The second step is to override the canMoveRowAt function in the data source:
      func tableView(_ tableView: UITableView, canMoveRowAt indexPath: 
       IndexPath) -> Bool { 
        return true 
      } 
  1. This will ask you if the row at the given indexPath can be moved or not. As all the rows in our app can be moved, just return true for all.
  2. The third step is to override the moveRowAt function, which is the function that performs the moving in ...

Get iOS Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.