【发布时间】:2016-08-04 19:40:48
【问题描述】:
要在 UICollectionView 中移动项目,我使用 UILongPressGestureRecognizer。
我实现了以下手势处理程序:
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()
}
}
加上对集合视图委托方法moveItemAtIndexPath 的正确覆盖,在一个部分内移动单元格完美。此外,将单元格移动到其他可见部分也可以按预期工作。但是,将单元格移动到通过平移/向下滚动变得可见的部分时会出现问题,导致以下错误(我使用 Realm 数据库):
'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).'
问题似乎与updateInteractiveMovementTargetPosition 中的手势识别器跟踪位置变化有关,因为此方法出现错误。
我想updateInteractiveMovementTargetPosition 直接调用集合视图方法moveItemAtIndexPath(不是通过委托),这会导致错误,因为中间单元格位置更改不会在 Realm 数据库中更新。还是我说的不对?
有人知道为什么在部分之间移动适用于可见部分,而不是在平移期间吗?以及如何正确处理滚动/平移?
【问题讨论】:
标签: ios uicollectionview realm uicollectionviewlayout