質問編集履歴

3

誤字の修正

2017/01/10 22:55

投稿

tarofess
tarofess

スコア127

test CHANGED
File without changes
test CHANGED
@@ -132,7 +132,7 @@
132
132
 
133
133
 
134
134
 
135
- 1.
135
+ 1:
136
136
 
137
137
  ```swift
138
138
 
@@ -172,11 +172,13 @@
172
172
 
173
173
 
174
174
 
175
- 2. はい、viewcontrollerのフィールドにstoryboardのtableviewから引っ張ってきたtableviewのインスタンスがあります。
175
+ 2: はい、viewcontrollerのフィールドにstoryboardのtableviewから引っ張ってきたtableviewのインスタンスがあります。
176
-
177
-
178
-
176
+
177
+
178
+
179
- 3.
179
+ 3:
180
+
181
+
180
182
 
181
183
  ```swift
182
184
 

2

回答を受けての追記part2

2017/01/10 22:55

投稿

tarofess
tarofess

スコア127

test CHANGED
File without changes
test CHANGED
@@ -86,7 +86,7 @@
86
86
 
87
87
 
88
88
 
89
- ```Swift
89
+ ```swift
90
90
 
91
91
  let realm = try! Realm()
92
92
 
@@ -125,3 +125,121 @@
125
125
  })
126
126
 
127
127
  ```
128
+
129
+
130
+
131
+ ///更なる追記///
132
+
133
+
134
+
135
+ 1.
136
+
137
+ ```swift
138
+
139
+ class NameEditTableViewCell: UITableViewCell {
140
+
141
+
142
+
143
+ @IBOutlet weak var nameTextField: UITextField!
144
+
145
+
146
+
147
+ }
148
+
149
+
150
+
151
+ class CategoryEditTableViewCell: UITableViewCell {
152
+
153
+
154
+
155
+ @IBOutlet weak var categoryTextField: UITextField!
156
+
157
+ }
158
+
159
+
160
+
161
+ class PriceEditTableViewCell: UITableViewCell {
162
+
163
+
164
+
165
+ @IBOutlet weak var priceTextField: UITextField!
166
+
167
+
168
+
169
+ }
170
+
171
+ ```
172
+
173
+
174
+
175
+ 2. はい、viewcontrollerのフィールドにstoryboardのtableviewから引っ張ってきたtableviewのインスタンスがあります。
176
+
177
+
178
+
179
+ 3.
180
+
181
+ ```swift
182
+
183
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
184
+
185
+ let type = self.displayCells()[indexPath.row]
186
+
187
+ let id = type.rawValue
188
+
189
+ let cell = self.tableView.dequeueReusableCell(withIdentifier: id, for: indexPath)
190
+
191
+ self.configureCell(targetCell: cell, type: type)
192
+
193
+
194
+
195
+ return cell
196
+
197
+ }
198
+
199
+
200
+
201
+ func configureCell(targetCell: UITableViewCell, type: CellType) {
202
+
203
+ switch type {
204
+
205
+ case .name:
206
+
207
+ if let cell = targetCell as? NameEditTableViewCell {
208
+
209
+ cell.nameTextField.text = self.product?.name
210
+
211
+ cell.nameTextField.delegate = self
212
+
213
+ }
214
+
215
+ case .category:
216
+
217
+ if let cell = targetCell as? CategoryEditTableViewCell {
218
+
219
+ cell.categoryTextField.text = self.product?.category
220
+
221
+ cell.categoryTextField.delegate = self
222
+
223
+ }
224
+
225
+ case .price:
226
+
227
+ if let cell = targetCell as? PriceEditTableViewCell {
228
+
229
+ if let price = self.product?.price.value {
230
+
231
+ cell.priceTextField.text = String(describing: price)
232
+
233
+ }
234
+
235
+ cell.priceTextField.delegate = self
236
+
237
+ }
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ ```

1

回答を受けて追記

2017/01/10 22:54

投稿

tarofess
tarofess

スコア127

test CHANGED
File without changes
test CHANGED
@@ -77,3 +77,51 @@
77
77
  realmへの値の保存を遅らせるだけでなく、その他何かいい方法があれば教えていただきたいです。
78
78
 
79
79
  よろしくお願いします。
80
+
81
+
82
+
83
+
84
+
85
+ ///回答を受けての追記///
86
+
87
+
88
+
89
+ ```Swift
90
+
91
+ let realm = try! Realm()
92
+
93
+ self.view.subviews.forEach({ view in
94
+
95
+ if let textField = view as? NameEditTableViewCell {
96
+
97
+ try! realm.write {
98
+
99
+ self.product?.name = textField.textLabel?.text
100
+
101
+ }
102
+
103
+ }
104
+
105
+ if let textField = view as? CategoryEditTableViewCell {
106
+
107
+ try! realm.write {
108
+
109
+ self.product?.name = textField.textLabel?.text
110
+
111
+ }
112
+
113
+ }
114
+
115
+ if let textField = view as? PriceEditTableViewCell {
116
+
117
+ try! realm.write {
118
+
119
+ self.product?.name = textField.textLabel?.text
120
+
121
+ }
122
+
123
+ }
124
+
125
+ })
126
+
127
+ ```