I know it\'s possible to reorder a single item/cell at a time when using the new UITableViewDropDelegate
and UITableViewDragDelegate
delegates but
I'm afraid I cannot go into much detail here, but here's basically how I was able to get it to work:
Multiple element reordering in array (example is for ints but can be modified to use indexpaths and datasource type:
// Multiple element reordering in array
func reorderList(list: Array, draggedIndices: Array, targetIndex: Int) -> Array {
// Defining the offsets that occur in destination and source indexes when we iterate over them one by one
var array = list
var draggedItemOffset = 0
var targetIndexOffset = 0
// Items being dragged ordered by their indexPaths
let orderedDragIndices = draggedIndices.sorted() // Add {$0.row < $1.row for sorting IndexPaths}
// Items being dragged ordered by their selection order
var selectionOrderDragItems = [Int]()
draggedIndices.forEach { (index) in
selectionOrderDragItems.append(array[index])
}
// Reordering the list
for i in (0...(orderedDragIndices.count - 1)).reversed() {
let removedItem = array.remove(at: orderedDragIndices[i] + draggedItemOffset)
array.insert(removedItem, at: targetIndex + targetIndexOffset)
if (i - 1 >= 0) && orderedDragIndices[i - 1] >= targetIndex {
draggedItemOffset += 1
} else {
draggedItemOffset = 0
targetIndexOffset -= 1
}
}
// Right now the dropped items are in the order of their source indexPaths. Returning them back to the order of selection
for index in Array(targetIndex + targetIndexOffset + 1...targetIndexOffset).sorted(by: >) {
array.remove(at: index)
}
array.insert(contentsOf: selectionOrderDragItems, at: targetIndex + targetIndexOffset + 1)
return array
}
In the dragDelegate and dropDelegate methods, I used single item reordering (UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)) to get the free animation where the other cells make space for a drop. However, as the user tapped on additional rows, I collected these additional indexPaths using didSelectRow while the drag session was active.
Using the above array of selected rows, in the performDrop delegate method, I used the algorithm described in (1) to restructure my datasource and reload the tableview section with animation.
I did some additional animation to show the user that rows are being collected under the finger using panGestureRecognizer and making a snapshot of the cell upon selection.
Note that I did not use canMoveRowAt or canEditRowAt or moveRowAt or other traditional methods in this process. I used the performDrop method from the iOS 11 APIs.
Hope it helps, and do reply if you find that the algorithm has some cases where it fails. Thanks and good luck!