2017-07-11 44 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() 
    } 

}

相關問題