LongPressReorder

LongPressReorder adresses a common use case when working with tables on an iOS device: the posibility to reorder table rows using a long press gesture, similar to drag and drop gesture. Lightweight and easy to use, LongPressReorder works with any view controller that manages an UITableView. Autoscroll and tables with one or multiple sections are supported.

This Swift module is based on the well-known article posted on raywenderlich.com:

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

UITableViewController with fixed first cell

UITableViewController with multiple sections

Usage

Because of the way Swift handles protocols and extensions, it’s extremely hard or next to impossible to add the desired behaviour directly on UIViewController or UITableView. Using the objc_setAssociatedObject hack is not an elegant Swift solution, therefore this module uses a wrapper of UITableView to achieve flexibility and cover all use cases.

Creation

First of all, import LongPressReorder into your view controller and declare a new variable

var reorderTableView: LongPressReorderTableView!

Assuming we are working with a UITableViewController, create the object in onViewDidLoad()

Reordering of cells is now purely visual. If you want to also change the model behind the table, LongPressReorder offers the following optional functions which can be grouped in a view controller extension

The module offers additional functions, which can be overridden in order to specify which cells cannot be moved or cannot lose their position inside the table

extension SpecificViewController {
override func startReorderingRow(atIndex indexPath: IndexPath) -> Bool {
// All table cells except the first one can be reordered
if indexPath.row > 0 {
return true
}
// First cell will not respond to the long press gesture
return false
}
override func allowChangingRow(atIndex indexPath: IndexPath) -> Bool {
// All table cells except the first one can be replaced with the selected cell
if indexPath.row > 0 {
return true
}
// First cell will not lose its position and therefore no other cell can replace it
return false
}
}

Customization

When there are more cells in the table that the ones that can be displayed on the device screen at a certain moment, autoscroll can be used when reordering the cells. Autoscroll is turned off by default.

After pressing on the desired cell, the cell will pop out of the table and will be ready to be dragged around. This pop out effect can be customized using 4 different scales for the selected cell: none, small, medium and big (default is medium).