質問編集履歴

4

ソースコード追加

2018/02/14 02:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -51,3 +51,107 @@
51
51
 
52
52
 
53
53
  配列とViewの整合性が取れていないためと思いますが、解決方法がわかりません。
54
+
55
+
56
+
57
+ ```swift
58
+
59
+
60
+
61
+ // データの定義
62
+
63
+ // 元データ
64
+
65
+ 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"]]
66
+
67
+ // セクションタイトル
68
+
69
+ var sectionItems: [String] = ["aItems","bItems","cItems","dItems","eItems"]
70
+
71
+ // 編集対象データ
72
+
73
+ var itemsData = [[String]]()
74
+
75
+
76
+
77
+
78
+
79
+ // viewDidLoadで編集対象データに元データを格納(なんとなくやっている)
80
+
81
+ override func viewDidLoad() {
82
+
83
+ super.viewDidLoad()
84
+
85
+ // Do any additional setup after loading the view, typically from a nib.
86
+
87
+ itemsData = itemsData0
88
+
89
+ addEventListner()
90
+
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ // 編集動作箇所 (見やすくするためアップしたファイルを簡略化)
106
+
107
+ func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
108
+
109
+    
110
+
111
+        // データソースの変更 
112
+
113
+ // 多次元配列から移動元セクション・移動先セクションの配列を取り出す 
114
+
115
+ var sourceDataDetail = itemsData[sourceIndexPath.section]
116
+
117
+ var destinationDataDetail = itemsData[destinationIndexPath.section]
118
+
119
+ // 移動対象セルを移動元セクションの配列から削除する
120
+
121
+        let tempStr = sourceDataDetail.remove(at: sourceIndexPath.item)
122
+
123
+ // 移動対象セルを移動先セクションの配列に挿入する
124
+
125
+        destinationDataDetail.insert(tempStr, at: destinationIndexPath.item)
126
+
127
+ // 移動先・移動元セクションを多次元配列に格納する
128
+
129
+        itemsData[sourceIndexPath.section] = sourceDataDetail
130
+
131
+ itemsData[destinationIndexPath.section] = destinationDataDetail
132
+
133
+
134
+
135
+        // CollectionViewe上のセルの編集(このへんかな?と思ってなんとなくやってます。)
136
+
137
+ collectionView.performBatchUpdates({ () -> Void in
138
+
139
+ self.collectionView.deleteItems(at: [sourceIndexPath])
140
+
141
+ self.collectionView.insertItems(at: [destinationIndexPath])
142
+
143
+ //self.collectionView.reloadData()
144
+
145
+ }, completion: nil)
146
+
147
+ self.collectionView.reloadData()
148
+
149
+ }
150
+
151
+
152
+
153
+ }
154
+
155
+
156
+
157
+ ```

3

画像変更

2018/02/14 02:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- !["E"を移動](7685465c82c3be85d33104e551ae70bb.png)
9
+ ![Cell”Eを移動](1f0483fe6997869ec5bf68f13e090bf0.png)
10
-
11
-
12
10
 
13
11
 
14
12
 

2

画像追加

2018/02/14 01:59

投稿

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

1

追記

2018/02/14 01:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -17,6 +17,8 @@
17
17
  https://github.com/tyobigoro/testDragCollectionView
18
18
 
19
19
 
20
+
21
+ swift4
20
22
 
21
23
  xcode version 9.1
22
24