回答編集履歴

2

修正

2016/08/01 16:37

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -25,3 +25,199 @@
25
25
  *** First throw call stack:
26
26
 
27
27
  ```
28
+
29
+
30
+
31
+ こちらで確認したコード
32
+
33
+ ---
34
+
35
+
36
+
37
+ ```swift
38
+
39
+ import UIKit
40
+
41
+
42
+
43
+ class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {
44
+
45
+
46
+
47
+ var labelList = [Label]()
48
+
49
+ @IBOutlet weak var tableView: UITableView!
50
+
51
+
52
+
53
+ override func viewDidLoad() {
54
+
55
+ super.viewDidLoad()
56
+
57
+
58
+
59
+ tableView.delegate = self
60
+
61
+ tableView.dataSource = self
62
+
63
+
64
+
65
+ let userDefaults = NSUserDefaults.standardUserDefaults()
66
+
67
+ if let labelListData = userDefaults.objectForKey(Constant.Label.LIST) as? NSData {
68
+
69
+ if let storedTodoList = NSKeyedUnarchiver.unarchiveObjectWithData(labelListData) as? [Label] {
70
+
71
+ labelList.appendContentsOf(storedTodoList)
72
+
73
+ }
74
+
75
+ }
76
+
77
+ }
78
+
79
+
80
+
81
+ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
82
+
83
+ return labelList.count;
84
+
85
+ }
86
+
87
+
88
+
89
+ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
90
+
91
+ let cell = tableView.dequeueReusableCellWithIdentifier("labelCell", forIndexPath: indexPath)
92
+
93
+ let label = labelList[indexPath.row]
94
+
95
+ cell.textLabel!.text = label.title
96
+
97
+ if label.done {
98
+
99
+ cell.accessoryType = UITableViewCellAccessoryType.Checkmark
100
+
101
+ } else {
102
+
103
+ cell.accessoryType = UITableViewCellAccessoryType.None
104
+
105
+ }
106
+
107
+ return cell
108
+
109
+ }
110
+
111
+
112
+
113
+
114
+
115
+ @IBAction func tapAddBtn(sender: UIButton) {
116
+
117
+ let alertController = UIAlertController(title: "ラベル追加", message: "ラベルを入力してください", preferredStyle: UIAlertControllerStyle.Alert)
118
+
119
+ alertController.addTextFieldWithConfigurationHandler(nil)
120
+
121
+ let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
122
+
123
+ (action: UIAlertAction) -> Void in
124
+
125
+ if let textField = alertController.textFields?.first {
126
+
127
+
128
+
129
+ let data = Label()
130
+
131
+ data.title = textField.text
132
+
133
+ self.labelList.insert(data, atIndex: 0)
134
+
135
+ self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Right)
136
+
137
+ self.setLabelList()
138
+
139
+ }
140
+
141
+ }
142
+
143
+ alertController.addAction(okAction)
144
+
145
+ let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Cancel, handler: nil)
146
+
147
+ alertController.addAction(cancelAction)
148
+
149
+ presentViewController(alertController, animated: true, completion: nil)
150
+
151
+ }
152
+
153
+
154
+
155
+ private func setLabelList() {
156
+
157
+ let data :NSData = NSKeyedArchiver.archivedDataWithRootObject(self.labelList)
158
+
159
+ let userDefaults = NSUserDefaults.standardUserDefaults()
160
+
161
+ userDefaults.setObject(data, forKey: Constant.Label.LIST)
162
+
163
+ userDefaults.synchronize()
164
+
165
+ }
166
+
167
+ }
168
+
169
+
170
+
171
+ class Constant {
172
+
173
+ class Label {
174
+
175
+ static let LIST = "labelList"
176
+
177
+ static let TITLE_KEY = "labelTitle"
178
+
179
+ static let DONE_KEY = "labelDone"
180
+
181
+ }
182
+
183
+ }
184
+
185
+
186
+
187
+ class Label: NSObject, NSCoding {
188
+
189
+ var title :String?
190
+
191
+ var done :Bool = false
192
+
193
+
194
+
195
+ override init() {
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+ required init?(coder aDecoder: NSCoder) {
204
+
205
+ title = aDecoder.decodeObjectForKey(Constant.Label.TITLE_KEY) as? String
206
+
207
+ done = aDecoder.decodeBoolForKey(Constant.Label.DONE_KEY)
208
+
209
+ }
210
+
211
+
212
+
213
+ func encodeWithCoder(aCoder: NSCoder) {
214
+
215
+ aCoder.encodeObject(title, forKey: Constant.Label.TITLE_KEY)
216
+
217
+ aCoder.encodeBool(done, forKey: Constant.Label.DONE_KEY)
218
+
219
+ }
220
+
221
+ }
222
+
223
+ ```

1

修正

2016/08/01 16:37

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -5,3 +5,23 @@
5
5
 
6
6
 
7
7
  [0から始めるiOS開発③: TODOアプリ開発(前編)](http://qiita.com/yukihirai0505/items/34b21a03f3216bc5c9ea)
8
+
9
+
10
+
11
+ エラー時のコンソールログに以下の様なものがあるのではないでしょうか。違う場合はエラーメッセージも載せて頂くと適切なアドバイスができると思います。
12
+
13
+
14
+
15
+ ```
16
+
17
+ *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
18
+
19
+ reason: 'unable to dequeue a cell with identifier labelCell -
20
+
21
+ must register a nib or a class for the identifier or connect a prototype cell
22
+
23
+ in a storyboard'
24
+
25
+ *** First throw call stack:
26
+
27
+ ```