gpt4 book ai didi

ios - UICollectionview 重新排序单元格崩溃 ios swift

转载 作者:行者123 更新时间:2023-11-30 12:19:04 25 4
gpt4 key购买 nike

我想重新排序 uicollection View 的单元格。当我尝试时,它有时可以工作(有滞后),但有时,我的应用程序崩溃了。我在网上到处搜索,但至今仍未找到答案。

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”状态,我认为这就是我的原因应用程序崩溃。

最佳答案

如果没有崩溃日志,很难确切地说出发生了什么,但同时这里有一些建议:

第一:

你的方法顶部有一个漂亮的守卫语句,我建议你添加let locationPoint: CGPoint! = panRecognizer.locationInView(collectionView) 到它。这样您就不必强制解包,并且代码将受到保护,免受此特定崩溃的影响。

第二:

当您调用 Collection View 的 endInteractiveMovement() 方法时,它会依次调用您的委托(delegate)方法 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()
}

}

关于ios - UICollectionview 重新排序单元格崩溃 ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45032603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com