collectionViewで他セクションへのセルの移動を行いたいのですが、
任意のセルを他のセクションにドラッグし、ドロップするとランタイムエラーになってしまいます。
同一セクションへの移動は可能です。
サンプルを上げてみました。
お時間ありましたらお願いします。
https://github.com/tyobigoro/testDragCollectionView
swift4
xcode version 9.1
エラー内容
testDragCollectionView[1142:451255]
*** Terminating app due to uncaught exception InternalInconsistencyException',
reason: 'Invalid update: invalid number of items in section 0.
The number of items contained in an existing section after the update (4) must be equal to the number of items contained in that section before the update (4),
plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted)
and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
配列とViewの整合性が取れていないためと思いますが、解決方法がわかりません。
swift
1 2// データの定義 3// 元データ 4let itemsData0: [[String]] = [["A","B","C","D","E"],["F","G","H","I","J"],["K","L","M","N","O"],["P","Q","R","S","T"],["U","V","W","X","Y"]] 5// セクションタイトル 6var sectionItems: [String] = ["aItems","bItems","cItems","dItems","eItems"] 7// 編集対象データ 8var itemsData = [[String]]() 9 10 11// viewDidLoadで編集対象データに元データを格納(なんとなくやっている) 12override func viewDidLoad() { 13 super.viewDidLoad() 14 // Do any additional setup after loading the view, typically from a nib. 15 itemsData = itemsData0 16 addEventListner() 17 } 18 19 20 21 22 23 24// 編集動作箇所 (見やすくするためアップしたファイルを簡略化) 25func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 26 27 // データソースの変更 28 // 多次元配列から移動元セクション・移動先セクションの配列を取り出す 29 var sourceDataDetail = itemsData[sourceIndexPath.section] 30 var destinationDataDetail = itemsData[destinationIndexPath.section] 31 // 移動対象セルを移動元セクションの配列から削除する 32 let tempStr = sourceDataDetail.remove(at: sourceIndexPath.item) 33 // 移動対象セルを移動先セクションの配列に挿入する 34 destinationDataDetail.insert(tempStr, at: destinationIndexPath.item) 35 // 移動先・移動元セクションを多次元配列に格納する 36 itemsData[sourceIndexPath.section] = sourceDataDetail 37 itemsData[destinationIndexPath.section] = destinationDataDetail 38 39 // CollectionViewe上のセルの編集(このへんかな?と思ってなんとなくやってます。) 40 collectionView.performBatchUpdates({ () -> Void in 41 self.collectionView.deleteItems(at: [sourceIndexPath]) 42 self.collectionView.insertItems(at: [destinationIndexPath]) 43 //self.collectionView.reloadData() 44 }, completion: nil) 45 self.collectionView.reloadData() 46 } 47 48 } 49
回答1件
あなたの回答
tips
プレビュー