Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reordering a UITableView: notification when user first starts dragging with the handles

I have a UITableView with cells that I want the user to be able to reorder by dragging them up & down, using the "handles" on the right side of each cell. I do NOT want to implement "drag & drop" functionality, because the data behind these cells makes no sense outside of this table (and implementing NSItemProvider looks to be an ugly process, given the data behind the table view & its cells). So far, everything is implemented & works fine. Looks somewhat like the results of this article.

The next step is, I'd like to know when the user has "lifted" or started to move a cell -- similar to the dragStateDidChange ability when one IS using drag & drop. I want to get this notification even before the cell has been moved out of its place -- as the UI turns the cell background white & "raises" it above the table.

How can I get this notification, without implementing "drag & drop"?

Things I've tried that don't accomplish what I want:

  • Recognizing a long-press gesture: I do use Gesture Recognizers on these cells for taps & such; it seems that when the user touches the drag handles to reorder the cells, this action does NOT fire a long-press Gesture. Tried that, no success.
  • Will-select-row-at: I implement the function func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? within the UITableViewDelegate code; it isn't called when the user touches the drag handles.
  • targetIndexPathForMoveFromRowAt: The func func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath fires when the dragged cell changes position, but not until then; it does NOT catch the start of the lift/drag process that I'm looking for.
  • Likewise, willBeginEditingRowAt and shouldHighlightRowAt aren't called either.
  • Various scrollView functions: they all get called when the user is scrolling the whole table, not when the user is reordering it.
  • Watching for "set editing": the drag handles need to always be present, so "editing" is always set to true.
  • Somewhat similar question, unfortunately without an answer that I can use (and also not in Swift).

I suspect that there must be a simple notification that the cell has been lifted and is being dragged using the handles, even if it hasn't been moved far enough to be above or below its old place yet. After all, the OS "knows" about this because it changes the background and adds shadows & so forth. Any help accessing that notification so that other code can respond to it as well would be most appreciated!

like image 333
ConfusionTowers Avatar asked Nov 06 '22 23:11

ConfusionTowers


1 Answers

It's a little strange that these are not documented, but you can define the following functions in your UITableViewController (or in the UITableViewDelegate) which will be called when cell dragging starts or ends, respectively:

@objc func tableView(_ tableView: UITableView, willBeginReorderingRowAtIndexPath indexPath: IndexPath) {
    // Dragging started
}

@objc func tableView(_ tableView: UITableView, didEndReorderingRowAtIndexPath indexPath: IndexPath) {
    // Dragging ended
}

like image 57
Andrew Bennet Avatar answered Nov 15 '22 05:11

Andrew Bennet