2017-07-11 111 views
0

我想重新排序我的单元格的uicollection视图。当我尝试时,它有时会起作用(延迟),但有时,我的应用程序崩溃。 我在互联网上到处搜索,但无法找到答案。UICollection视图重新排序单元格崩溃ios swift

func handleLongGesture(panRecognizer: UIPanGestureRecognizer){ 

    let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView) 
    guard let selectedIndexPath : NSIndexPath = collectionView.indexPathForItemAtPoint(locationPoint) else { 
     return 
    } 
    if panRecognizer.state == .Began{ 

     collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath) 
     indexPathSelectedItem = selectedIndexPath 
    } 
    else if panRecognizer.state == .Changed{ 

     collectionView.updateInteractiveMovementTargetPosition(locationPoint) 

    } 
    else if panRecognizer.state == .Ended{ 

     collectionView.endInteractiveMovement() 
    } 
} 

这是我正在尝试上面的代码。我无法找到整个代码中的错误。 我想让你知道,我也尝试使用断点来找出我的应用程序崩溃的地方,我发现有时控制不能在状态“panRecognizer.state == .Ended”下去,我认为这是原因我的应用崩溃了。

+1

哪里是你的symbolicated崩溃日志?哪一行代码导致崩溃? –

+0

你还可以添加你的崩溃日志吗? – Thomas

回答

0

没有崩溃日志就很难说究竟发生了什么事,但这里有在此期间一些建议:

第一:

你在你的方法顶部有一个美丽的后卫声明,我建议你添加let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView)它。通过这种方式,您不必强制解包并且代码将受到保护以防止此特定崩溃。

二:

当你打电话给你集合视图的endInteractiveMovement()方法,它会反过来,现在你需要更新你的数据源,并有移动的项目,以及调用您的委托方法collectionView:moveItemAtIndexPath:toIndexPath:让你。

确保你已经实现了它,并将有问题的对象移动到正确的位置!如果没有,您的应用程序将崩溃,因为数据源不再与collectionview同步。

我建议你使用,而不是一个switch语句中的if-else赶上其他所有可能的状态,这会给您取消移动操作(你不是在做正确的可能性现在):

switch(panRecognizer.state) { 

    case UIGestureRecognizerState.Began: 
     // Begin movement 
     collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath) 
    indexPathSelectedItem = selectedIndexPath 

    case UIGestureRecognizerState.Changed: 
     // Update movement 
     collectionView.updateInteractiveMovementTargetPosition(locationPoint) 

    case UIGestureRecognizerState.Ended: 
     // End movement 
     collectionView.endInteractiveMovement() 

    default: 
     collectionView.cancelInteractiveMovement() 
    } 

}

相关问题