質問編集履歴
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -45,3 +45,195 @@
|
|
45
45
|
}
|
46
46
|
|
47
47
|
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
全体のコードです
|
52
|
+
|
53
|
+
```Swift
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
import UIKit
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
@IBOutlet var textInput: UITextField!
|
66
|
+
|
67
|
+
@IBOutlet var table: UITableView!
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
//var fridgeItem = [String]() // this is going to init this array so i don't need to put anything in it.
|
74
|
+
|
75
|
+
//var freezerItem = [String]()
|
76
|
+
|
77
|
+
//var otherItem = [String]()
|
78
|
+
|
79
|
+
var items = [[String](),[String](),[String]()]
|
80
|
+
|
81
|
+
//Fridge, Freezer, Other
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
//refered:http://cabbalog.blogspot.com/2016/09/xcode8-swift3-nsuserdefaults.html
|
86
|
+
|
87
|
+
let ud = [UserDefaults.standard,UserDefaults.standard,UserDefaults.standard]
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
@IBOutlet var fridgeSegOutlet: UISegmentedControl!
|
92
|
+
|
93
|
+
@IBAction func fridgeSeg(_ sender: Any) {
|
94
|
+
|
95
|
+
//if ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) != nil{
|
96
|
+
|
97
|
+
//items[fridgeSegOutlet.selectedSegmentIndex] = ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) as! [String]
|
98
|
+
|
99
|
+
//}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
table.reloadData()
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
@IBAction func addItem(_ sender: Any) {
|
112
|
+
|
113
|
+
// Add item to list
|
114
|
+
|
115
|
+
if textInput.text != "" {
|
116
|
+
|
117
|
+
items[fridgeSegOutlet.selectedSegmentIndex].append(textInput.text!)
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
// Clear text input field
|
122
|
+
|
123
|
+
textInput.text = ""
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
// refresh table view
|
128
|
+
|
129
|
+
table.reloadData()
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
// save into memory
|
134
|
+
|
135
|
+
ud[fridgeSegOutlet.selectedSegmentIndex].set(items[fridgeSegOutlet.selectedSegmentIndex], forKey: String(fridgeSegOutlet.selectedSegmentIndex))
|
136
|
+
|
137
|
+
self.textInput.resignFirstResponder()
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
override func viewDidLoad() {
|
144
|
+
|
145
|
+
super.viewDidLoad()
|
146
|
+
|
147
|
+
if ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) != nil{
|
148
|
+
|
149
|
+
//items[fridgeSegOutlet.selectedSegmentIndex] = ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex)) as! [String]
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
//for index in 0...2{
|
154
|
+
|
155
|
+
// ud[index].set(items[index], forKey: String(index))
|
156
|
+
|
157
|
+
//}
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
return items[fridgeSegOutlet.selectedSegmentIndex].count
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
cell.textLabel?.text = items[fridgeSegOutlet.selectedSegmentIndex][indexPath.row]
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
return cell
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
|
194
|
+
|
195
|
+
if editingStyle == UITableViewCellEditingStyle.delete{
|
196
|
+
|
197
|
+
items[fridgeSegOutlet.selectedSegmentIndex].remove(at: indexPath.row)
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
table.deleteRows(at: [indexPath], with: .automatic)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
ud[fridgeSegOutlet.selectedSegmentIndex].object(forKey: String(fridgeSegOutlet.selectedSegmentIndex))
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
214
|
+
|
215
|
+
self.view.endEditing(true)
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
override func didReceiveMemoryWarning() {
|
222
|
+
|
223
|
+
super.didReceiveMemoryWarning()
|
224
|
+
|
225
|
+
// Dispose of any resources that can be recreated.
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
```
|