teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2016/08/01 16:37

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -11,4 +11,102 @@
11
11
  must register a nib or a class for the identifier or connect a prototype cell
12
12
  in a storyboard'
13
13
  *** First throw call stack:
14
+ ```
15
+
16
+ こちらで確認したコード
17
+ ---
18
+
19
+ ```swift
20
+ import UIKit
21
+
22
+ class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {
23
+
24
+ var labelList = [Label]()
25
+ @IBOutlet weak var tableView: UITableView!
26
+
27
+ override func viewDidLoad() {
28
+ super.viewDidLoad()
29
+
30
+ tableView.delegate = self
31
+ tableView.dataSource = self
32
+
33
+ let userDefaults = NSUserDefaults.standardUserDefaults()
34
+ if let labelListData = userDefaults.objectForKey(Constant.Label.LIST) as? NSData {
35
+ if let storedTodoList = NSKeyedUnarchiver.unarchiveObjectWithData(labelListData) as? [Label] {
36
+ labelList.appendContentsOf(storedTodoList)
37
+ }
38
+ }
39
+ }
40
+
41
+ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
42
+ return labelList.count;
43
+ }
44
+
45
+ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
46
+ let cell = tableView.dequeueReusableCellWithIdentifier("labelCell", forIndexPath: indexPath)
47
+ let label = labelList[indexPath.row]
48
+ cell.textLabel!.text = label.title
49
+ if label.done {
50
+ cell.accessoryType = UITableViewCellAccessoryType.Checkmark
51
+ } else {
52
+ cell.accessoryType = UITableViewCellAccessoryType.None
53
+ }
54
+ return cell
55
+ }
56
+
57
+
58
+ @IBAction func tapAddBtn(sender: UIButton) {
59
+ let alertController = UIAlertController(title: "ラベル追加", message: "ラベルを入力してください", preferredStyle: UIAlertControllerStyle.Alert)
60
+ alertController.addTextFieldWithConfigurationHandler(nil)
61
+ let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
62
+ (action: UIAlertAction) -> Void in
63
+ if let textField = alertController.textFields?.first {
64
+
65
+ let data = Label()
66
+ data.title = textField.text
67
+ self.labelList.insert(data, atIndex: 0)
68
+ self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Right)
69
+ self.setLabelList()
70
+ }
71
+ }
72
+ alertController.addAction(okAction)
73
+ let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Cancel, handler: nil)
74
+ alertController.addAction(cancelAction)
75
+ presentViewController(alertController, animated: true, completion: nil)
76
+ }
77
+
78
+ private func setLabelList() {
79
+ let data :NSData = NSKeyedArchiver.archivedDataWithRootObject(self.labelList)
80
+ let userDefaults = NSUserDefaults.standardUserDefaults()
81
+ userDefaults.setObject(data, forKey: Constant.Label.LIST)
82
+ userDefaults.synchronize()
83
+ }
84
+ }
85
+
86
+ class Constant {
87
+ class Label {
88
+ static let LIST = "labelList"
89
+ static let TITLE_KEY = "labelTitle"
90
+ static let DONE_KEY = "labelDone"
91
+ }
92
+ }
93
+
94
+ class Label: NSObject, NSCoding {
95
+ var title :String?
96
+ var done :Bool = false
97
+
98
+ override init() {
99
+
100
+ }
101
+
102
+ required init?(coder aDecoder: NSCoder) {
103
+ title = aDecoder.decodeObjectForKey(Constant.Label.TITLE_KEY) as? String
104
+ done = aDecoder.decodeBoolForKey(Constant.Label.DONE_KEY)
105
+ }
106
+
107
+ func encodeWithCoder(aCoder: NSCoder) {
108
+ aCoder.encodeObject(title, forKey: Constant.Label.TITLE_KEY)
109
+ aCoder.encodeBool(done, forKey: Constant.Label.DONE_KEY)
110
+ }
111
+ }
14
112
  ```

1

修正

2016/08/01 16:37

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,4 +1,14 @@
1
1
  おそらくですが、参考にしているサイトの一つ前の段階での工程で`UITableViewCell`に対して`IdentifierのところにlabelCellと入力する。`というところの作業が抜けているでは無いでしょうか?
2
2
  確認してください。
3
3
 
4
- [0から始めるiOS開発③: TODOアプリ開発(前編)](http://qiita.com/yukihirai0505/items/34b21a03f3216bc5c9ea)
4
+ [0から始めるiOS開発③: TODOアプリ開発(前編)](http://qiita.com/yukihirai0505/items/34b21a03f3216bc5c9ea)
5
+
6
+ エラー時のコンソールログに以下の様なものがあるのではないでしょうか。違う場合はエラーメッセージも載せて頂くと適切なアドバイスができると思います。
7
+
8
+ ```
9
+ *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
10
+ reason: 'unable to dequeue a cell with identifier labelCell -
11
+ must register a nib or a class for the identifier or connect a prototype cell
12
+ in a storyboard'
13
+ *** First throw call stack:
14
+ ```