標籤:ack 測試 get int fun sel ges lin with
本文
功能
用 UICollectionView 實現兩個 cell 之間的位置互動或者拖拽某個位置刪除
問題
iOS 11 以上拖拽刪除會崩潰,在 iOS 9、10 都沒有問題
錯誤
017-10-11 11:38:02.692004+0800 MOCR[2585:1047221] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘attempting to invalidate an item at an invalid indexPath: <NSIndexPath: 0x1c442a680> {length = 2, path = 0 - 1} globalIndex: 1 numItems: 1‘
*** First throw call stack:
(0x181f3bd38 0x181450528 0x181f3bc0c 0x1828cac24 0x18be1091c 0x18bdd2ab8 0x18b4b74ac 0x18b4b48b8 0x18b569a4c 0x18bdd9e98 0x10275d49c 0x10275d45c 0x102762050 0x181ee3f20 0x181ee1afc 0x181e022d8 0x183c93f84 0x18b3af880 0x1009a753c 0x18192656c)
libc++abi.dylib: terminating with uncaught exception of type NSException
代碼
func handleLongGesture(gesture: UILongPressGestureRecognizer) { switch(gesture.state) { case .began: guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else { break } collectionView.beginInteractiveMovementForItem(at: selectedIndexPath) case .changed: collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!)) case .ended: self.collectionView.endInteractiveMovement() // 檢測是否刪除操作,是的話刪除資料並調用 reloadData() default: collectionView.cancelInteractiveMovement() }}
分析
測試發現調用 beginInteractiveMovementForItem 和 endInteractiveMovement 也會觸發 reloadData 操作,這樣刪除前後會調用兩次 reloadData,但是 reloadData 又是非同步作業,所以就報錯了。
解決
func handleLongGesture(gesture: UILongPressGestureRecognizer) { switch(gesture.state) { case .began: guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else { break } collectionView.beginInteractiveMovementForItem(at: selectedIndexPath) case .changed: collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!)) case .ended: self.collectionView.endInteractiveMovement() DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300), execute: { [weak self] in // 檢測是否刪除操作,是的話刪除資料並調用 reloadData() }) default: collectionView.cancelInteractiveMovement() }}
加一個延遲處理就行
【Swift 4.0】iOS 11 UICollectionView 長按拖拽刪除崩潰的問題