teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

ソースコード追加

2018/02/14 02:24

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -24,4 +24,56 @@
24
24
  plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted)
25
25
  and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
26
26
 
27
- 配列とViewの整合性が取れていないためと思いますが、解決方法がわかりません。
27
+ 配列とViewの整合性が取れていないためと思いますが、解決方法がわかりません。
28
+
29
+ ```swift
30
+
31
+ // データの定義
32
+ // 元データ
33
+ let 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"]]
34
+ // セクションタイトル
35
+ var sectionItems: [String] = ["aItems","bItems","cItems","dItems","eItems"]
36
+ // 編集対象データ
37
+ var itemsData = [[String]]()
38
+
39
+
40
+ // viewDidLoadで編集対象データに元データを格納(なんとなくやっている)
41
+ override func viewDidLoad() {
42
+ super.viewDidLoad()
43
+ // Do any additional setup after loading the view, typically from a nib.
44
+ itemsData = itemsData0
45
+ addEventListner()
46
+ }
47
+
48
+
49
+
50
+
51
+
52
+
53
+ // 編集動作箇所 (見やすくするためアップしたファイルを簡略化)
54
+ func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
55
+    
56
+        // データソースの変更 
57
+ // 多次元配列から移動元セクション・移動先セクションの配列を取り出す 
58
+ var sourceDataDetail = itemsData[sourceIndexPath.section]
59
+ var destinationDataDetail = itemsData[destinationIndexPath.section]
60
+ // 移動対象セルを移動元セクションの配列から削除する
61
+        let tempStr = sourceDataDetail.remove(at: sourceIndexPath.item)
62
+ // 移動対象セルを移動先セクションの配列に挿入する
63
+        destinationDataDetail.insert(tempStr, at: destinationIndexPath.item)
64
+ // 移動先・移動元セクションを多次元配列に格納する
65
+        itemsData[sourceIndexPath.section] = sourceDataDetail
66
+ itemsData[destinationIndexPath.section] = destinationDataDetail
67
+
68
+        // CollectionViewe上のセルの編集(このへんかな?と思ってなんとなくやってます。)
69
+ collectionView.performBatchUpdates({ () -> Void in
70
+ self.collectionView.deleteItems(at: [sourceIndexPath])
71
+ self.collectionView.insertItems(at: [destinationIndexPath])
72
+ //self.collectionView.reloadData()
73
+ }, completion: nil)
74
+ self.collectionView.reloadData()
75
+ }
76
+
77
+ }
78
+
79
+ ```

3

画像変更

2018/02/14 02:23

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -2,10 +2,9 @@
2
2
  任意のセルを他のセクションにドラッグし、ドロップするとランタイムエラーになってしまいます。
3
3
  同一セクションへの移動は可能です。
4
4
 
5
- !["E"を移動](7685465c82c3be85d33104e551ae70bb.png)
5
+ ![Cell”Eを移動](1f0483fe6997869ec5bf68f13e090bf0.png)
6
6
 
7
7
 
8
-
9
8
  サンプルを上げてみました。
10
9
  お時間ありましたらお願いします。
11
10
 

2

画像追加

2018/02/14 01:59

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,10 @@
2
2
  任意のセルを他のセクションにドラッグし、ドロップするとランタイムエラーになってしまいます。
3
3
  同一セクションへの移動は可能です。
4
4
 
5
+ !["E"を移動](7685465c82c3be85d33104e551ae70bb.png)
5
6
 
7
+
8
+
6
9
  サンプルを上げてみました。
7
10
  お時間ありましたらお願いします。
8
11
 

1

追記

2018/02/14 01:51

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -8,6 +8,7 @@
8
8
 
9
9
  https://github.com/tyobigoro/testDragCollectionView
10
10
 
11
+ swift4
11
12
  xcode version 9.1
12
13
 
13
14