質問編集履歴
2
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,4 +2,140 @@
|
|
2
2
|
|
3
3
|
並べ替えられる機能は実装できたのですが、並べ替えが保存されずビルドし直すと、並べ替え前の順番に戻ってしまっています。
|
4
4
|
並び順を保持する方法は何かありませんか?
|
5
|
-
ご教示よろしくお願いします。
|
5
|
+
ご教示よろしくお願いします。
|
6
|
+
|
7
|
+
```swift
|
8
|
+
import UIKit
|
9
|
+
|
10
|
+
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
|
11
|
+
|
12
|
+
var collectionView: UICollectionView?
|
13
|
+
var longPressGesture: UILongPressGestureRecognizer?
|
14
|
+
var selectedIndexPath: NSIndexPath?
|
15
|
+
|
16
|
+
override func viewDidLoad() {
|
17
|
+
super.viewDidLoad()
|
18
|
+
self.setupSubviews()
|
19
|
+
self.autolayoutSubviews()
|
20
|
+
|
21
|
+
self.navigationItem.rightBarButtonItem = self.editButtonItem()
|
22
|
+
}
|
23
|
+
|
24
|
+
func setupSubviews() {
|
25
|
+
|
26
|
+
let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
|
27
|
+
flowLayout.minimumInteritemSpacing = 10.0
|
28
|
+
flowLayout.minimumLineSpacing = 10.0
|
29
|
+
flowLayout.sectionInset = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
|
30
|
+
flowLayout.itemSize = CGSizeMake(300.0, 100.0)
|
31
|
+
|
32
|
+
self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
|
33
|
+
self.collectionView!.translatesAutoresizingMaskIntoConstraints = false
|
34
|
+
self.collectionView!.dataSource = self
|
35
|
+
self.collectionView!.delegate = self
|
36
|
+
|
37
|
+
self.collectionView!.registerClass(FirstCell.self, forCellWithReuseIdentifier: "FirstCell")
|
38
|
+
self.collectionView!.registerClass(SecondCell.self, forCellWithReuseIdentifier: "SecondCell")
|
39
|
+
self.collectionView!.registerClass(ThirdCell.self, forCellWithReuseIdentifier: "ThirdCell")
|
40
|
+
|
41
|
+
self.collectionView!.backgroundColor = UIColor.whiteColor()
|
42
|
+
self.view.addSubview(self.collectionView!)
|
43
|
+
|
44
|
+
longPressGesture = UILongPressGestureRecognizer(target: self, action:
|
45
|
+
#selector(ViewController.handleLongGesture(_:)))
|
46
|
+
|
47
|
+
|
48
|
+
longPressGesture?.minimumPressDuration = 0.5
|
49
|
+
|
50
|
+
self.longPressGesture!.enabled = false
|
51
|
+
|
52
|
+
|
53
|
+
self.collectionView?.addGestureRecognizer(self.longPressGesture!)
|
54
|
+
}
|
55
|
+
|
56
|
+
func autolayoutSubviews() {
|
57
|
+
|
58
|
+
self.collectionView!.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
|
59
|
+
self.collectionView!.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true
|
60
|
+
self.collectionView!.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true
|
61
|
+
self.collectionView!.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
override func setEditing(editing: Bool, animated: Bool) {
|
66
|
+
super.setEditing(editing, animated: animated)
|
67
|
+
|
68
|
+
self.collectionView!.allowsMultipleSelection = editing
|
69
|
+
let indexPaths: [NSIndexPath] = self.collectionView!.indexPathsForVisibleItems()
|
70
|
+
|
71
|
+
for indexPath in indexPaths {
|
72
|
+
self.collectionView!.deselectItemAtIndexPath(indexPath, animated: false)
|
73
|
+
let cell: UICollectionViewCell = self.collectionView!.cellForItemAtIndexPath(indexPath)!
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
if editing {
|
78
|
+
self.longPressGesture!.enabled = true
|
79
|
+
}
|
80
|
+
else {
|
81
|
+
self.longPressGesture!.enabled = false
|
82
|
+
}
|
83
|
+
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
89
|
+
return 3
|
90
|
+
}
|
91
|
+
|
92
|
+
|
93
|
+
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
|
94
|
+
let cellTypeNumber = indexPath.item % 3
|
95
|
+
switch cellTypeNumber {
|
96
|
+
case 0:
|
97
|
+
let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier("FirstCell", forIndexPath: indexPath) as! FirstCell
|
98
|
+
cell1.backgroundColor = UIColor.redColor()
|
99
|
+
return cell1
|
100
|
+
|
101
|
+
case 1:
|
102
|
+
let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("SecondCell", forIndexPath: indexPath) as! SecondCell
|
103
|
+
cell2.backgroundColor = UIColor.yellowColor()
|
104
|
+
return cell2
|
105
|
+
|
106
|
+
default:
|
107
|
+
let cell3 = collectionView.dequeueReusableCellWithReuseIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdCell
|
108
|
+
cell3.backgroundColor = UIColor.greenColor()
|
109
|
+
return cell3
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
|
114
|
+
}
|
115
|
+
|
116
|
+
|
117
|
+
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
|
118
|
+
|
119
|
+
switch(gesture.state) {
|
120
|
+
case UIGestureRecognizerState.Began:
|
121
|
+
guard let selectedIndexPath = collectionView?.indexPathForItemAtPoint(gesture.locationInView(collectionView!)) else {
|
122
|
+
break
|
123
|
+
}
|
124
|
+
collectionView?.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
|
125
|
+
case UIGestureRecognizerState.Changed:
|
126
|
+
collectionView?.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
|
127
|
+
case UIGestureRecognizerState.Ended:
|
128
|
+
collectionView?.endInteractiveMovement()
|
129
|
+
default:
|
130
|
+
collectionView?.cancelInteractiveMovement()
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
```
|
1
質問の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
CollectionViewのcellの並べ替えを保存したいです。
|
2
2
|
|
3
3
|
並べ替えられる機能は実装できたのですが、並べ替えが保存されずビルドし直すと、並べ替え前の順番に戻ってしまっています。
|
4
|
-
|
4
|
+
並び順を保持する方法は何かありませんか?
|
5
|
-
|
6
|
-
|
5
|
+
ご教示よろしくお願いします。
|