質問編集履歴

2

ソースコードの更新

2019/05/26 15:00

投稿

pftyuk
pftyuk

スコア52

test CHANGED
File without changes
test CHANGED
@@ -30,37 +30,57 @@
30
30
 
31
31
  ```Swift
32
32
 
33
- class AddCostsViewController: UIViewController {
33
+ class InputCostsViewController: UIViewController {
34
+
35
+
36
+
34
-
37
+ var items:[UtilityCosts] = [
38
+
35
-
39
+ UtilityCosts.Electricity,
40
+
36
-
41
+ UtilityCosts.Gas,
42
+
43
+ UtilityCosts.Water
44
+
45
+ ]
46
+
47
+
48
+
49
+ var result:[UtilityCosts:String] = [:]
50
+
51
+
52
+
37
- @IBOutlet weak var addCostTableView: UITableView!
53
+ @IBOutlet weak var inputCostsTableView: UITableView!{
54
+
38
-
55
+ didSet{
39
-
40
-
56
+
41
- let item = ["電気代","ガス代","水道代"]
57
+ let nib = UINib(nibName: InputCostsTableViewCell.identifire, bundle: nil)
42
-
43
-
44
-
58
+
45
-   //追記
59
+ inputCostsTableView.register(nib, forCellReuseIdentifier: InputCostsTableViewCell.identifire)
46
-
60
+
61
+
62
+
47
- var result:[InputCostsTableViewCell] = []
63
+ inputCostsTableView.dataSource = self
48
-
49
-
50
-
64
+
65
+
66
+
51
- @IBAction func addCosts(_ sender: UIButton) {
67
+ inputCostsTableView.allowsSelection = false
52
-
53
-      //追記
68
+
54
-
55
- result.forEach { (cell) in
56
-
57
- print(cell.textField.text!)
69
+ inputCostsTableView.tableFooterView = UIView(frame: .zero)
58
70
 
59
71
  }
60
72
 
61
-
73
+ }
74
+
75
+
76
+
62
-
77
+ @IBAction func RegisterCosts(_ sender: UIButton) {
78
+
79
+ print(result)
80
+
81
+
82
+
63
- self.navigationController?.popViewController(animated: true)
83
+ //navigationController?.popViewController(animated: true)
64
84
 
65
85
  }
66
86
 
@@ -70,62 +90,32 @@
70
90
 
71
91
  super.viewDidLoad()
72
92
 
93
+ }
94
+
95
+ }
96
+
97
+
98
+
99
+ extension InputCostsViewController:UITableViewDataSource{
100
+
101
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
102
+
103
+ return items.count
104
+
105
+ }
106
+
107
+
108
+
109
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
110
+
111
+ let cell = tableView.dequeueReusableCell(withIdentifier: InputCostsTableViewCell.identifire, for: indexPath) as! InputCostsTableViewCell
112
+
113
+ cell.configure(kind: items[indexPath.row])
114
+
115
+ cell.delegate = self
116
+
73
117
 
74
118
 
75
- let nib = UINib(nibName: "InputCostsTableViewCell", bundle: nil)
76
-
77
- addCostTableView.register(nib, forCellReuseIdentifier: "InputCostsTableViewCell")
78
-
79
-
80
-
81
- addCostTableView.delegate = self
82
-
83
- addCostTableView.dataSource = self
84
-
85
-
86
-
87
- addCostTableView.allowsSelection = false
88
-
89
- addCostTableView.tableFooterView = UIView(frame: .zero)
90
-
91
- }
92
-
93
- }
94
-
95
-
96
-
97
- extension AddCostsViewController:UITableViewDelegate{
98
-
99
-
100
-
101
- }
102
-
103
-
104
-
105
- extension AddCostsViewController:UITableViewDataSource{
106
-
107
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
108
-
109
- return item.count
110
-
111
- }
112
-
113
-
114
-
115
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
116
-
117
- let cell = tableView.dequeueReusableCell(withIdentifier: "InputCostsTableViewCell", for: indexPath) as! InputCostsTableViewCell
118
-
119
- cell.configure(placeholder: item[indexPath.row])
120
-
121
-
122
-
123
- //追記
124
-
125
- result.append(cell)
126
-
127
-
128
-
129
119
  return cell
130
120
 
131
121
  }
@@ -140,15 +130,57 @@
140
130
 
141
131
  }
142
132
 
133
+
134
+
135
+ extension InputCostsViewController:TextFieldEditCellDelegate{
136
+
137
+ func textFieldEditingChanged(cell: InputCostsTableViewCell, value: String) {
138
+
139
+ let key = cell.kind
140
+
141
+ result.updateValue(value, forKey: key)
142
+
143
+ }
144
+
145
+ }
146
+
143
147
  ```
144
148
 
145
149
  ```Swift
146
150
 
151
+ protocol TextFieldEditCellDelegate:class {
152
+
153
+ func textFieldEditingChanged(cell:InputCostsTableViewCell,value:String)
154
+
155
+ }
156
+
157
+
158
+
147
159
  class InputCostsTableViewCell: UITableViewCell {
148
160
 
149
161
 
150
162
 
163
+ @IBOutlet weak var icon: UIImageView!
164
+
151
- @IBOutlet weak var textField: UITextField!
165
+ @IBOutlet weak var textField: UITextField!{
166
+
167
+ didSet{
168
+
169
+ textField.borderStyle = .none
170
+
171
+ textField.keyboardType = .numberPad
172
+
173
+ }
174
+
175
+ }
176
+
177
+
178
+
179
+ var kind = UtilityCosts.None
180
+
181
+ weak var delegate:TextFieldEditCellDelegate? = nil
182
+
183
+ static let identifire = "InputCostsTableViewCell"
152
184
 
153
185
 
154
186
 
@@ -156,11 +188,7 @@
156
188
 
157
189
  super.awakeFromNib()
158
190
 
159
-
160
-
161
- textField.borderStyle = .none
162
-
163
- textField.keyboardType = .numberPad
191
+ textField.addTarget(self, action: #selector(editingChanged), for: .editingChanged)
164
192
 
165
193
  }
166
194
 
@@ -174,20 +202,100 @@
174
202
 
175
203
 
176
204
 
177
- func configure(placeholder:String){
205
+ func configure(kind:UtilityCosts){
206
+
178
-
207
+ self.kind = kind
208
+
209
+ self.icon.image = kind.icon
210
+
179
- textField.placeholder = placeholder
211
+ self.textField.placeholder = kind.name
180
-
212
+
181
- }
213
+ }
214
+
215
+
216
+
182
-
217
+ @objc func editingChanged(textField: UITextField) {
218
+
219
+ self.delegate?.textFieldEditingChanged(cell: self, value: textField.text!)
220
+
183
- }
221
+ }
222
+
184
-
223
+ }
185
-
186
224
 
187
225
  ```
188
226
 
189
227
 
190
228
 
229
+ ```Swift
230
+
231
+ enum UtilityCosts{
232
+
233
+ case None
234
+
235
+ case Electricity
236
+
237
+ case Gas
238
+
239
+ case Water
240
+
241
+
242
+
243
+ var name:String{
244
+
245
+ switch self {
246
+
247
+ case .None:
248
+
249
+ return ""
250
+
251
+ case .Electricity:
252
+
253
+ return "電気代"
254
+
255
+ case . Gas:
256
+
257
+ return "ガス代"
258
+
259
+ case .Water:
260
+
261
+ return "水道代"
262
+
263
+ }
264
+
265
+ }
266
+
267
+
268
+
269
+ var icon:UIImage?{
270
+
271
+ switch self {
272
+
273
+ case .None:
274
+
275
+ return nil
276
+
277
+ case .Electricity:
278
+
279
+ return nil
280
+
281
+ case . Gas:
282
+
283
+ return nil
284
+
285
+ case .Water:
286
+
287
+ return nil
288
+
289
+ }
290
+
291
+ }
292
+
293
+ }
294
+
295
+ ```
296
+
297
+
298
+
191
299
  ### 試したこと
192
300
 
193
301
  調べたことを記載します。

1

ソースの追記

2019/05/26 15:00

投稿

pftyuk
pftyuk

スコア52

test CHANGED
File without changes
test CHANGED
@@ -40,15 +40,25 @@
40
40
 
41
41
  let item = ["電気代","ガス代","水道代"]
42
42
 
43
+
44
+
45
+   //追記
46
+
47
+ var result:[InputCostsTableViewCell] = []
48
+
43
49
 
44
50
 
45
51
  @IBAction func addCosts(_ sender: UIButton) {
46
52
 
47
- /*
53
+      //追記
54
+
48
-
55
+ result.forEach { (cell) in
56
+
49
- ここで各textFieldに入力された値を取得したい
57
+ print(cell.textField.text!)
50
-
58
+
51
- */
59
+ }
60
+
61
+
52
62
 
53
63
  self.navigationController?.popViewController(animated: true)
54
64
 
@@ -108,6 +118,12 @@
108
118
 
109
119
  cell.configure(placeholder: item[indexPath.row])
110
120
 
121
+
122
+
123
+ //追記
124
+
125
+ result.append(cell)
126
+
111
127
 
112
128
 
113
129
  return cell