简体   繁体   English

UICollection视图重新排序单元格使iOS Swift崩溃

[英]UICollection view Reordering cells crash ios swift

I want to reorder my cells of uicollection view. 我想对uicollection视图的单元格进行重新排序。 When i try, it works sometimes(with lags) but sometimes, my app got crashed. 当我尝试时,它有时会工作(有滞后),但有时我的应用程序崩溃了。 I searched everywhere on internet but unable to find the answer till now. 我在互联网上到处搜索,但直到现在都找不到答案。

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()
    }
}

This is the above code which i am trying. 这是我正在尝试的上述代码。 i am not able to find the bugs in the entire code. 我无法找到整个代码中的错误。 I want to let you know that i also tried to use breakpoints to find out where my app crashing and i found that sometimes control not able to go under the state "panRecognizer.state == .Ended" and i think that is the reason my app get crashing. 我想让您知道,我也尝试使用断点来找出我的应用程序崩溃的位置,并且我发现有时控件无法进入“ panRecognizer.state == .Ended”状态,这就是我的原因。应用程序崩溃。

Without the crash log it's difficult to say exactly what's happening but here are a few suggestions in the meanwhile: 没有崩溃日志,很难确切说明正在发生什么,但是与此同时,这里有一些建议:

First: 第一:

You have a beautiful guard statement at the top of your method, I suggest you add the let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView) 您在方法的顶部有一个漂亮的警卫声明,建议您添加let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView) let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView) to it. let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView) This way you don't have to force unwrap and the code will be protected against this particular crash. 这样,您就不必强行解开包装,并且可以保护代码免受这种特殊崩溃的影响。

Second: 第二:

When you call the endInteractiveMovement() method of your collection view, it will in turn call your delegate method collectionView:moveItemAtIndexPath:toIndexPath: to let you now that you need to update your datasource and move the item there as well. 当您调用集合视图的endInteractiveMovement()方法时,它将依次调用委托方法collectionView:moveItemAtIndexPath:toIndexPath:以便您现在需要更新数据源并将该项目也移到那里。

Make sure you implemented it and moved the object in question to the right place! 确保实现它并将有问题的对象移到正确的位置! If not, your app will crash because the datasource is not in sync with the collectionview anymore. 否则,您的应用将崩溃,因为数据源不再与collectionview同步。

Third 第三

I suggest you use a switch statement instead of a if-else to catch all other possible states, this will give you the possibility to cancel the move action (which you're not doing right now): 我建议您使用switch语句而不是if-else来捕获所有其他可能的状态,这将使您有可能取消move操作(您现在不执行此操作):

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()
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM