Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell reordering drag action unexpectedly stops

I'm trying to figure out how to debug the following scenario to understand why UITableView cells won't re-order.

  1. Touch 'Edit' menu item, to put table view into edit mode.
  2. Edit mode works for deleting cells.
  3. Try to re-order cells by touching and dragging the re-order control on a cell.
  4. Cell outline moves in the drag direction a couple of pixels, but then snaps back into it's original position even through my finger is still dragging across the screen.

In code:

  • UITableViewDelegate: targetIndexPathForMoveFromRowAtIndexPath never gets called. This would seem consistent with the UI feedback for the cell snapping back to it's original position before it ever gets on top of another cell.
  • UITableViewDataSource: canMoveRowAtIndexPath returns true for all rows.
  • UITableViewDataSource: moveRowAtIndexPath always gets called as soon as the cell snaps back to it's original position, and the sourceIndexPath and destinationIndexPath values are the same. -

The same view controller code and storyboard work correctly for reordering cells in another project.

I've searched stackoverflow and Google for a day and can't find any reference to what would cause a cell drag event to get canceled.

Any thoughts on how I could approach debugging this issue would be greatly appreciated!

like image 579
navigator48 Avatar asked Aug 25 '16 17:08

navigator48


2 Answers

I found the answer, and it had nothing to do with the UITableView code.

My code has a programmatically created root view controller that handles showing/hiding a sliding menu panel.

The viewDidLoad() of this view controller creates a panGestureRecognizer to track swipes for showing/hiding the sliding menu. This panGestureRecognizer was not passing through touches.

Adding this code fixed the problem:

panGestureRecognizer.cancelsTouchesInView = false
like image 112
navigator48 Avatar answered Oct 05 '22 05:10

navigator48


I experienced the same issue, and found the solution in my case was to add the various swipe & tap gestures on my tableview cells NOT to the cell itself, but to the contentView of the cell.

So, rather than this,

cell.addGestureRecognizer(myGesture)

the code becomes

cell.contentView.addGestureRecognizer(myGesture)

And that seems to work well, fixing the same sort of herky-jerky scrolling that the OP had. Perhaps somebody can benefit from this solution, if your issue is more like the one I was having than the one the OP had.

Longer discussion of the solution can be found here: Add a gestureRecognizer to a tableView cell

like image 41
ConfusionTowers Avatar answered Oct 05 '22 05:10

ConfusionTowers