nicolasgomollon / LPRTableView

A drop-in replacement for UITableView and UITableViewController that supports long-press reordering of cells.
MIT License
126 stars 24 forks source link

Method for Committing Reorder Changes #25

Open cliftonlabrum opened 6 years ago

cliftonlabrum commented 6 years ago

This is really nice, thank you! :)

I know that the moveRowAt method fires when you reorder the cells, but it fires each time the row moves, even if the user hasn't let go yet.

Is there a method that fires when a user lets go of the cell and commits the reorder change? That's when I'd like to save the data to my database.

nicolasgomollon commented 6 years ago

Thanks, I’m glad you’ve found this project useful!

Yes, there is, but I’ll admit it’s a little convoluted to figure out. Perhaps this part of the process can be refactored (and simplified into one method) in the future.

The following two delegate methods are called when the user picks up a cell, and drops a cell, respectively:

// Called when the user picks up a cell.
func tableView(_ tableView: UITableView, showDraggingView view: UIView, at indexPath: IndexPath)

// Called when the user drops a cell.
func tableView(_ tableView: UITableView, hideDraggingView view: UIView, at indexPath: IndexPath)

Essentially, you’d have to keep track of the source IndexPath from within the first method, and then the destination IndexPath would be provided in the second method. At which point you could commit the changes to a database from within the second method, provided the cell wasn’t just picked up and dropped at the same IndexPath.

Please leave this issue open, so I don’t forget to refactor this part of the code.