質問編集履歴
1
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,4 +21,100 @@
|
|
21
21
|
|
22
22
|
self.textInput.resignFirstResponder()
|
23
23
|
}
|
24
|
+
```
|
25
|
+
|
26
|
+
全体のコードです
|
27
|
+
```Swift
|
28
|
+
|
29
|
+
import UIKit
|
30
|
+
|
31
|
+
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
32
|
+
|
33
|
+
@IBOutlet var textInput: UITextField!
|
34
|
+
@IBOutlet var table: UITableView!
|
35
|
+
|
36
|
+
|
37
|
+
//var fridgeItem = [String]() // this is going to init this array so i don't need to put anything in it.
|
38
|
+
//var freezerItem = [String]()
|
39
|
+
//var otherItem = [String]()
|
40
|
+
var items = [[String](),[String](),[String]()]
|
41
|
+
//Fridge, Freezer, Other
|
42
|
+
|
43
|
+
//refered:http://cabbalog.blogspot.com/2016/09/xcode8-swift3-nsuserdefaults.html
|
44
|
+
let ud = [UserDefaults.standard,UserDefaults.standard,UserDefaults.standard]
|
45
|
+
|
46
|
+
@IBOutlet var fridgeSegOutlet: UISegmentedControl!
|
47
|
+
@IBAction func fridgeSeg(_ sender: Any) {
|
48
|
+
//if ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) != nil{
|
49
|
+
//items[fridgeSegOutlet.selectedSegmentIndex] = ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) as! [String]
|
50
|
+
//}
|
51
|
+
|
52
|
+
|
53
|
+
table.reloadData()
|
54
|
+
}
|
55
|
+
|
56
|
+
@IBAction func addItem(_ sender: Any) {
|
57
|
+
// Add item to list
|
58
|
+
if textInput.text != "" {
|
59
|
+
items[fridgeSegOutlet.selectedSegmentIndex].append(textInput.text!)
|
60
|
+
}
|
61
|
+
// Clear text input field
|
62
|
+
textInput.text = ""
|
63
|
+
|
64
|
+
// refresh table view
|
65
|
+
table.reloadData()
|
66
|
+
|
67
|
+
// save into memory
|
68
|
+
ud[fridgeSegOutlet.selectedSegmentIndex].set(items[fridgeSegOutlet.selectedSegmentIndex], forKey: String(fridgeSegOutlet.selectedSegmentIndex))
|
69
|
+
self.textInput.resignFirstResponder()
|
70
|
+
}
|
71
|
+
|
72
|
+
override func viewDidLoad() {
|
73
|
+
super.viewDidLoad()
|
74
|
+
if ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) != nil{
|
75
|
+
//items[fridgeSegOutlet.selectedSegmentIndex] = ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) as! [String]
|
76
|
+
}
|
77
|
+
//for index in 0...2{
|
78
|
+
// ud[index].set(items[index], forKey: String(index))
|
79
|
+
//}
|
80
|
+
}
|
81
|
+
|
82
|
+
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
|
83
|
+
|
84
|
+
return items[fridgeSegOutlet.selectedSegmentIndex].count
|
85
|
+
}
|
86
|
+
|
87
|
+
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
|
88
|
+
|
89
|
+
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
|
90
|
+
|
91
|
+
cell.textLabel?.text = items[fridgeSegOutlet.selectedSegmentIndex][indexPath.row]
|
92
|
+
|
93
|
+
return cell
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
|
98
|
+
if editingStyle == UITableViewCellEditingStyle.delete{
|
99
|
+
items[fridgeSegOutlet.selectedSegmentIndex].remove(at: indexPath.row)
|
100
|
+
|
101
|
+
table.deleteRows(at: [indexPath], with: .automatic)
|
102
|
+
|
103
|
+
ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex))
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
108
|
+
self.view.endEditing(true)
|
109
|
+
}
|
110
|
+
|
111
|
+
override func didReceiveMemoryWarning() {
|
112
|
+
super.didReceiveMemoryWarning()
|
113
|
+
// Dispose of any resources that can be recreated.
|
114
|
+
}
|
115
|
+
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
24
120
|
```
|