To move items in a UICollectionView I use a UILongPressGestureRecognizer.
I implemented the following gesture handler:
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.Began:
guard let selectedIndexPath = self.collectionView.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {break}
collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
case UIGestureRecognizerState.Changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}
Together with a correct override of the collection view delegate method moveItemAtIndexPath, moving cells within a section works perfect. Furthermore, moving cells to other visible sections also works as expected. However, problems occur when moving cells to sections that become visible by panning/scrolling down, resulting in the following error (I use a Realm database):
'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (0), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
The problem seems related to the tracking of location changes by the gesture recognizer in updateInteractiveMovementTargetPosition since the error occurs in this method.
I suppose that updateInteractiveMovementTargetPosition directly calls the collection view method moveItemAtIndexPath (not via the delegate) which results in an error since intermediate cell location changes are not updated in the Realm database. Or am I incorrect?
Would anybody know why moving between sections works for visible sections, but not during panning? And how should the scrolling/panning be handled correctly?