質問編集履歴

2

コードの編集

2016/09/01 01:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -42,11 +42,11 @@
42
42
 
43
43
  struct SerializedKey {
44
44
 
45
- static let patientcell = "PatientCell"
45
+ static let cell1 = "Cell1"
46
46
 
47
- static let nursecell = "NurseCell"
47
+ static let cell2 = "Cell2"
48
48
 
49
- static let gocell = "GoCell"
49
+ static let cell3 = "Cell3"
50
50
 
51
51
  }
52
52
 
@@ -56,21 +56,21 @@
56
56
 
57
57
 
58
58
 
59
- var PatientCell: UICollectionViewCell
59
+ var Cell1: UICollectionViewCell
60
60
 
61
- var NurseCell: UICollectionViewCell
61
+ var Cell2: UICollectionViewCell
62
62
 
63
- var GoCell: UICollectionViewCell
63
+ var Cell3: UICollectionViewCell
64
64
 
65
65
 
66
66
 
67
- init(PatientCell: UICollectionViewCell, NurseCell: UICollectionViewCell, GoCell: UICollectionViewCell) {
67
+ init(Cell1: UICollectionViewCell, Cell2: UICollectionViewCell, Cell3: UICollectionViewCell) {
68
68
 
69
- self.PatientCell = PatientCell
69
+ self.Cell1 = Cell1
70
70
 
71
- self.NurseCell = NurseCell
71
+ self.Cell2 = Cell2
72
72
 
73
- self.GoCell = GoCell
73
+ self.Cell3 = Cell3
74
74
 
75
75
  }
76
76
 
@@ -80,11 +80,11 @@
80
80
 
81
81
 
82
82
 
83
- self.PatientCell = (aDecoder.decodeObjectForKey(SerializedKey.patientcell) as? UICollectionViewCell)!
83
+ self.Cell1 = (aDecoder.decodeObjectForKey(SerializedKey.cell1) as? UICollectionViewCell)!
84
84
 
85
- self.NurseCell = (aDecoder.decodeObjectForKey(SerializedKey.nursecell) as? UICollectionViewCell)!
85
+ self.Cell2 = (aDecoder.decodeObjectForKey(SerializedKey.cell2) as? UICollectionViewCell)!
86
86
 
87
- self.GoCell = (aDecoder.decodeObjectForKey(SerializedKey.gocell) as? UICollectionViewCell)!
87
+ self.Cell3 = (aDecoder.decodeObjectForKey(SerializedKey.cell3) as? UICollectionViewCell)!
88
88
 
89
89
  }
90
90
 
@@ -92,11 +92,11 @@
92
92
 
93
93
  func encodeWithCoder(aCoder: NSCoder) {
94
94
 
95
- aCoder.encodeObject(self.PatientCell, forKey: SerializedKey.patientcell)
95
+ aCoder.encodeObject(self.Cell1, forKey: SerializedKey.cell1)
96
96
 
97
- aCoder.encodeObject(self.NurseCell, forKey: SerializedKey.nursecell)
97
+ aCoder.encodeObject(self.Cell2, forKey: SerializedKey.cell2)
98
98
 
99
- aCoder.encodeObject(self.GoCell, forKey: SerializedKey.gocell)
99
+ aCoder.encodeObject(self.Cell3, forKey: SerializedKey.cell3)
100
100
 
101
101
  }
102
102
 
@@ -118,11 +118,11 @@
118
118
 
119
119
 
120
120
 
121
- func register(PatientCell: UICollectionViewCell, NurseCell: UICollectionViewCell, GoCell: UICollectionViewCell) {
121
+ func register(Cell1: UICollectionViewCell, Cell2: UICollectionViewCell, Cell3: UICollectionViewCell) {
122
122
 
123
123
 
124
124
 
125
- let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(Cell(PatientCell: PatientCell, NurseCell: NurseCell, GoCell: GoCell))
125
+ let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(Cell(Cell1: Cell1, Cell2: Cell2, Cell3: Cell3))
126
126
 
127
127
  self.userDefaults.setObject(archivedObject, forKey: UserDefaultsKey.Cell)
128
128
 

1

コードを更新しました。

2016/09/01 01:42

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -32,61 +32,71 @@
32
32
 
33
33
  ```swift
34
34
 
35
- //セルの順序を保存する
35
+ struct UserDefaultsKey {
36
36
 
37
- func saveCustomLayout(sender: AnyObject) {
38
-
39
-
40
-
41
- //ユーザデフォルトを参照する
42
-
43
- let defaults = NSUserDefaults.standardUserDefaults()
44
-
45
-
46
-
47
- //過去にレイアウトが変更されているか確認
48
-
49
- if((defaults.objectForKey("CustomLayout")) != nil) {
50
-
51
- //鍵"CustomLayout"でセルの順番を保存する
52
-
53
- defaults.setValue(collectionView, forKey: "CustomLayout")
37
+ static let Cell = "cell"
54
-
55
- } else {
56
-
57
- //もともとの順番のまま
58
-
59
- }
60
-
61
-
62
-
63
- //配列をopenKeyで保存
64
-
65
- defaults.setObject(collectionView, forKey: "CustomLayout")
66
-
67
- defaults.synchronize()
68
-
69
-
70
38
 
71
39
  }
72
40
 
73
41
 
74
42
 
75
- //ユーザデフォルトから読み込む
43
+ struct SerializedKey {
76
44
 
77
- func readCustomLayout(sender: AnyObject) {
45
+ static let patientcell = "PatientCell"
78
46
 
79
- //ユーザデフォルトを参照する
47
+ static let nursecell = "NurseCell"
80
48
 
81
- let defaults = NSUserDefaults.standardUserDefaults()
49
+ static let gocell = "GoCell"
82
50
 
83
- //鍵"CustomLayout"をオブジェクととして読み込む
51
+ }
84
52
 
85
- if let MyLayout = defaults.objectForKey("CustomLayout") {
53
+
86
54
 
87
- //セルの順番がMyLayout
55
+ class Cell: NSObject, NSCoding {
88
56
 
57
+
58
+
89
- collectionView = MyLayout as! UICollectionView
59
+ var PatientCell: UICollectionViewCell
60
+
61
+ var NurseCell: UICollectionViewCell
62
+
63
+ var GoCell: UICollectionViewCell
64
+
65
+
66
+
67
+ init(PatientCell: UICollectionViewCell, NurseCell: UICollectionViewCell, GoCell: UICollectionViewCell) {
68
+
69
+ self.PatientCell = PatientCell
70
+
71
+ self.NurseCell = NurseCell
72
+
73
+ self.GoCell = GoCell
74
+
75
+ }
76
+
77
+
78
+
79
+ required init?(coder aDecoder: NSCoder) {
80
+
81
+
82
+
83
+ self.PatientCell = (aDecoder.decodeObjectForKey(SerializedKey.patientcell) as? UICollectionViewCell)!
84
+
85
+ self.NurseCell = (aDecoder.decodeObjectForKey(SerializedKey.nursecell) as? UICollectionViewCell)!
86
+
87
+ self.GoCell = (aDecoder.decodeObjectForKey(SerializedKey.gocell) as? UICollectionViewCell)!
88
+
89
+ }
90
+
91
+
92
+
93
+ func encodeWithCoder(aCoder: NSCoder) {
94
+
95
+ aCoder.encodeObject(self.PatientCell, forKey: SerializedKey.patientcell)
96
+
97
+ aCoder.encodeObject(self.NurseCell, forKey: SerializedKey.nursecell)
98
+
99
+ aCoder.encodeObject(self.GoCell, forKey: SerializedKey.gocell)
90
100
 
91
101
  }
92
102
 
@@ -96,6 +106,56 @@
96
106
 
97
107
 
98
108
 
109
+ class CustomLayout {
110
+
111
+
112
+
113
+ static let sharedInstance = CustomLayout()
114
+
115
+
116
+
117
+ private let userDefaults = NSUserDefaults.standardUserDefaults()
118
+
119
+
120
+
121
+ func register(PatientCell: UICollectionViewCell, NurseCell: UICollectionViewCell, GoCell: UICollectionViewCell) {
122
+
123
+
124
+
125
+ let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(Cell(PatientCell: PatientCell, NurseCell: NurseCell, GoCell: GoCell))
126
+
127
+ self.userDefaults.setObject(archivedObject, forKey: UserDefaultsKey.Cell)
128
+
129
+ self.userDefaults.synchronize()
130
+
131
+ }
132
+
133
+
134
+
135
+ var registeredCell: Cell? {
136
+
137
+
138
+
139
+ guard let unarchivedObject = self.userDefaults.objectForKey(UserDefaultsKey.Cell) as? NSData,
140
+
141
+ let cell = NSKeyedUnarchiver.unarchiveObjectWithData(unarchivedObject) as? Cell else {
142
+
143
+
144
+
145
+
146
+
147
+ return nil
148
+
149
+ }
150
+
151
+
152
+
153
+ return cell
154
+
155
+ }
156
+
157
+ }
158
+
99
159
 
100
160
 
101
161
  ```