質問編集履歴
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,3 +7,275 @@
|
|
7
7
|
並び順を保持する方法は何かありませんか?
|
8
8
|
|
9
9
|
ご教示よろしくお願いします。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```swift
|
14
|
+
|
15
|
+
import UIKit
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
var collectionView: UICollectionView?
|
24
|
+
|
25
|
+
var longPressGesture: UILongPressGestureRecognizer?
|
26
|
+
|
27
|
+
var selectedIndexPath: NSIndexPath?
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
override func viewDidLoad() {
|
32
|
+
|
33
|
+
super.viewDidLoad()
|
34
|
+
|
35
|
+
self.setupSubviews()
|
36
|
+
|
37
|
+
self.autolayoutSubviews()
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
self.navigationItem.rightBarButtonItem = self.editButtonItem()
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
func setupSubviews() {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
|
52
|
+
|
53
|
+
flowLayout.minimumInteritemSpacing = 10.0
|
54
|
+
|
55
|
+
flowLayout.minimumLineSpacing = 10.0
|
56
|
+
|
57
|
+
flowLayout.sectionInset = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
|
58
|
+
|
59
|
+
flowLayout.itemSize = CGSizeMake(300.0, 100.0)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
|
64
|
+
|
65
|
+
self.collectionView!.translatesAutoresizingMaskIntoConstraints = false
|
66
|
+
|
67
|
+
self.collectionView!.dataSource = self
|
68
|
+
|
69
|
+
self.collectionView!.delegate = self
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
self.collectionView!.registerClass(FirstCell.self, forCellWithReuseIdentifier: "FirstCell")
|
74
|
+
|
75
|
+
self.collectionView!.registerClass(SecondCell.self, forCellWithReuseIdentifier: "SecondCell")
|
76
|
+
|
77
|
+
self.collectionView!.registerClass(ThirdCell.self, forCellWithReuseIdentifier: "ThirdCell")
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
self.collectionView!.backgroundColor = UIColor.whiteColor()
|
82
|
+
|
83
|
+
self.view.addSubview(self.collectionView!)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
longPressGesture = UILongPressGestureRecognizer(target: self, action:
|
88
|
+
|
89
|
+
#selector(ViewController.handleLongGesture(_:)))
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
longPressGesture?.minimumPressDuration = 0.5
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
self.longPressGesture!.enabled = false
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
self.collectionView?.addGestureRecognizer(self.longPressGesture!)
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
func autolayoutSubviews() {
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
self.collectionView!.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
|
116
|
+
|
117
|
+
self.collectionView!.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true
|
118
|
+
|
119
|
+
self.collectionView!.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true
|
120
|
+
|
121
|
+
self.collectionView!.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
override func setEditing(editing: Bool, animated: Bool) {
|
130
|
+
|
131
|
+
super.setEditing(editing, animated: animated)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
self.collectionView!.allowsMultipleSelection = editing
|
136
|
+
|
137
|
+
let indexPaths: [NSIndexPath] = self.collectionView!.indexPathsForVisibleItems()
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
for indexPath in indexPaths {
|
142
|
+
|
143
|
+
self.collectionView!.deselectItemAtIndexPath(indexPath, animated: false)
|
144
|
+
|
145
|
+
let cell: UICollectionViewCell = self.collectionView!.cellForItemAtIndexPath(indexPath)!
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
if editing {
|
154
|
+
|
155
|
+
self.longPressGesture!.enabled = true
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
else {
|
160
|
+
|
161
|
+
self.longPressGesture!.enabled = false
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
176
|
+
|
177
|
+
return 3
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
|
186
|
+
|
187
|
+
let cellTypeNumber = indexPath.item % 3
|
188
|
+
|
189
|
+
switch cellTypeNumber {
|
190
|
+
|
191
|
+
case 0:
|
192
|
+
|
193
|
+
let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier("FirstCell", forIndexPath: indexPath) as! FirstCell
|
194
|
+
|
195
|
+
cell1.backgroundColor = UIColor.redColor()
|
196
|
+
|
197
|
+
return cell1
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
case 1:
|
202
|
+
|
203
|
+
let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("SecondCell", forIndexPath: indexPath) as! SecondCell
|
204
|
+
|
205
|
+
cell2.backgroundColor = UIColor.yellowColor()
|
206
|
+
|
207
|
+
return cell2
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
default:
|
212
|
+
|
213
|
+
let cell3 = collectionView.dequeueReusableCellWithReuseIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdCell
|
214
|
+
|
215
|
+
cell3.backgroundColor = UIColor.greenColor()
|
216
|
+
|
217
|
+
return cell3
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
switch(gesture.state) {
|
238
|
+
|
239
|
+
case UIGestureRecognizerState.Began:
|
240
|
+
|
241
|
+
guard let selectedIndexPath = collectionView?.indexPathForItemAtPoint(gesture.locationInView(collectionView!)) else {
|
242
|
+
|
243
|
+
break
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
collectionView?.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
|
248
|
+
|
249
|
+
case UIGestureRecognizerState.Changed:
|
250
|
+
|
251
|
+
collectionView?.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
|
252
|
+
|
253
|
+
case UIGestureRecognizerState.Ended:
|
254
|
+
|
255
|
+
collectionView?.endInteractiveMovement()
|
256
|
+
|
257
|
+
default:
|
258
|
+
|
259
|
+
collectionView?.cancelInteractiveMovement()
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```
|
1
質問の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
|
5
5
|
並べ替えられる機能は実装できたのですが、並べ替えが保存されずビルドし直すと、並べ替え前の順番に戻ってしまっています。
|
6
6
|
|
7
|
-
|
7
|
+
並び順を保持する方法は何かありませんか?
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ご教示よろしくお願いします。
|