質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

3796閲覧

collectionViewで他セクションへのセルの移動を行うとランタイムエラーになる。

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/02/14 01:05

編集2018/02/14 02:24

collectionViewで他セクションへのセルの移動を行いたいのですが、
任意のセルを他のセクションにドラッグし、ドロップするとランタイムエラーになってしまいます。
同一セクションへの移動は可能です。

Cell”E”を移動

サンプルを上げてみました。
お時間ありましたらお願いします。

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/02/14 03:59

reloadData()を呼ばなければ落ちないでしょうか?
退会済みユーザー

退会済みユーザー

2018/02/14 04:16

確か最初は”reloadData()”なしで落ちていたので後から追加したはずです。有る無しで挙動が若干違ったような。確認します。
退会済みユーザー

退会済みユーザー

2018/02/14 04:22

reloadData()なしでも挙動同じで、ランタイムエラーでした。
退会済みユーザー

退会済みユーザー

2018/02/14 04:29 編集

もうしわけありません。  ↓を削除したら動きました 時間をさいてもらってしまってもうしわけありませんでした。 ただ、動かなかったから記述を追加したはずなのですが... // CollectionViewe上のセルの編集(このへんかな?と思ってなんとなくやってます。) collectionView.performBatchUpdates({ () -> Void in self.collectionView.deleteItems(at: [sourceIndexPath]) self.collectionView.insertItems(at: [destinationIndexPath]) //self.collectionView.reloadData() }, completion: nil) self.collectionView.reloadData() } を削除したら動きました。
guest

回答1

0

ベストアンサー

↓コードを一部削除
// CollectionViewe上のセルの編集
collectionView.performBatchUpdates({ () -> Void in
self.collectionView.deleteItems(at: [sourceIndexPath])
self.collectionView.insertItems(at: [destinationIndexPath])
//self.collectionView.reloadData()
}, completion: nil)
self.collectionView.reloadData()
}

投稿2018/02/14 06:51

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問