質問編集履歴

3

具体的に実現したいことを書き加えました。

2017/02/06 06:26

投稿

blakekei
blakekei

スコア35

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,8 @@
1
- 永続的にデータを保存したくcoredataを書き加えたのですがエラーが起きしまいどこが間違っているのか分からないのです
1
+ 永続的にデータを保存したのですがエラーが起きしまいどこが間違っているのか分からないのです
2
+
3
+ coredataのENTITYSでEntityとEntity1を作りviewcontroller.swiftに書き加えたのですが多分どこかの入力ミスでエラーが起きたのだと思います。
4
+
5
+ やりたいこととしては入力したデータをアプリを閉じても消えないようにしたいです。
2
6
 
3
7
  ```swift
4
8
 
@@ -18,7 +22,7 @@
18
22
 
19
23
  @IBOutlet weak var tableView: UITableView!
20
24
 
21
-
25
+ //coreデータのEntity
22
26
 
23
27
  var sectionTitleArray = [Entity]()
24
28
 
@@ -98,7 +102,9 @@
98
102
 
99
103
 
100
104
 
105
+ //ここでエラーが起きた
106
+
101
- self.sectionTitleArray[Entity].insert(text, at: 0)
107
+ self.sectionTitleArray[Entity].insert(text, at: 0)
102
108
 
103
109
  self.dataArrayGroup.insert([], at: 0)
104
110
 
@@ -182,6 +188,8 @@
182
188
 
183
189
  !text.isEmpty,
184
190
 
191
+ //ここでもエラー
192
+
185
193
  let index = self.sectionTitleArray[Entity].index(of: text) {
186
194
 
187
195
 
@@ -360,6 +368,8 @@
360
368
 
361
369
  } else {
362
370
 
371
+ //ここでもエラーが起きる
372
+
363
373
  return sectionTitleArray[Entity]
364
374
 
365
375
  }
@@ -463,3 +473,39 @@
463
473
  }
464
474
 
465
475
  ```
476
+
477
+ ```swift
478
+
479
+ import CoreData
480
+
481
+ import UIKit
482
+
483
+
484
+
485
+ class CustomCell: UITableViewCell {
486
+
487
+
488
+
489
+ @IBOutlet weak var indexLabel: UILabel!
490
+
491
+ @IBOutlet weak var countLabel: UILabel!
492
+
493
+ @IBOutlet weak var priceLabel: UILabel!
494
+
495
+
496
+
497
+ var data: Entity1! {
498
+
499
+ didSet {
500
+
501
+ countLabel.text = data.count
502
+
503
+ priceLabel.text = data.price
504
+
505
+ }
506
+
507
+ }
508
+
509
+ }
510
+
511
+ ```

2

内容変更

2017/02/06 06:26

投稿

blakekei
blakekei

スコア35

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,465 @@
1
1
  永続的にデータを保存したくcoredataを書き加えたのですがエラーが起きしまいどこが間違っているのか分からないのです
2
2
 
3
+ ```swift
4
+
5
+ //viewcontroller.swift
6
+
7
+ import CoreData
8
+
9
+ import UIKit
10
+
11
+
12
+
13
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
14
+
15
+
16
+
17
+
18
+
19
+ @IBOutlet weak var tableView: UITableView!
20
+
21
+
22
+
23
+ var sectionTitleArray = [Entity]()
24
+
25
+ var dataArrayGroup: [[Entity1]] = []
26
+
27
+
28
+
29
+ override func viewDidLoad() {
30
+
31
+ super.viewDidLoad()
32
+
33
+
34
+
35
+ tableView.estimatedRowHeight = 44
36
+
37
+ tableView.rowHeight = UITableViewAutomaticDimension
38
+
39
+ tableView.tableFooterView = UIView()
40
+
41
+ }
42
+
43
+
44
+
45
+
46
+
47
+ @IBAction func addSection(_ sender: UIButton) {
48
+
49
+
50
+
51
+ let alert = UIAlertController(title:"Sction追加",
52
+
53
+ message: "SectionTitleを入力してください",
54
+
55
+ preferredStyle: .alert)
56
+
57
+
58
+
59
+ let cancelAction = UIAlertAction(title: "Cancel",
60
+
61
+ style: .cancel,
62
+
63
+ handler:
64
+
65
+ { action -> Void in
66
+
67
+ print("Cancel")
68
+
69
+ })
70
+
71
+
72
+
73
+ let defaultAction = UIAlertAction(title: "OK",
74
+
75
+ style: .default,
76
+
77
+ handler:
78
+
79
+ { action -> Void in
80
+
81
+
82
+
83
+ // TextFieldから値を取得
84
+
85
+ if let textFields = alert.textFields {
86
+
87
+ for textField in textFields {
88
+
89
+
90
+
91
+ if let text = textField.text, !text.isEmpty {
92
+
93
+
94
+
95
+ // 取得したテキストをセクションのタイトルとして追加する
96
+
97
+ print(text)
98
+
99
+
100
+
101
+ self.sectionTitleArray[Entity].insert(text, at: 0)
102
+
103
+ self.dataArrayGroup.insert([], at: 0)
104
+
105
+ self.tableView.insertSections(IndexSet(integer: 0), with: .automatic)
106
+
107
+ }
108
+
109
+ }
110
+
111
+ }
112
+
113
+ })
114
+
115
+
116
+
117
+ alert.addAction(cancelAction)
118
+
119
+ alert.addAction(defaultAction)
120
+
121
+ alert.addTextField(configurationHandler: { text -> Void in })
122
+
123
+ present(alert, animated: true, completion: nil)
124
+
125
+ }
126
+
127
+
128
+
129
+ @IBAction func deleteSection(_ sender: UIButton) {
130
+
131
+
132
+
133
+ if sectionTitleArray.isEmpty {
134
+
135
+ return
136
+
137
+ }
138
+
139
+
140
+
141
+ let alert = UIAlertController(title:"Sction削除",
142
+
143
+ message: "削除するSectionTitleを入力してください",
144
+
145
+ preferredStyle: .alert)
146
+
147
+
148
+
149
+ let cancelAction = UIAlertAction(title: "Cancel",
150
+
151
+ style: .cancel,
152
+
153
+ handler:
154
+
155
+ { action -> Void in
156
+
157
+ print("Cancel")
158
+
159
+ })
160
+
161
+
162
+
163
+ let defaultAction = UIAlertAction(title: "OK",
164
+
165
+ style: .default,
166
+
167
+ handler:
168
+
169
+ { action -> Void in
170
+
171
+
172
+
173
+ // TextFieldから値を取得
174
+
175
+ if let textFields = alert.textFields {
176
+
177
+ for textField in textFields {
178
+
179
+
180
+
181
+ if let text = textField.text,
182
+
183
+ !text.isEmpty,
184
+
185
+ let index = self.sectionTitleArray[Entity].index(of: text) {
186
+
187
+
188
+
189
+ self.sectionTitleArray.remove(at: index)
190
+
191
+ self.dataArrayGroup.remove(at: index)
192
+
193
+ self.tableView.deleteSections(IndexSet(integer: index), with: .automatic)
194
+
195
+ }
196
+
197
+ }
198
+
199
+ }
200
+
201
+ })
202
+
203
+
204
+
205
+ alert.addAction(cancelAction)
206
+
207
+ alert.addAction(defaultAction)
208
+
209
+ alert.addTextField(configurationHandler: { text -> Void in })
210
+
211
+ present(alert, animated: true, completion: nil)
212
+
213
+ }
214
+
215
+
216
+
217
+ @IBOutlet weak var deletetext: UITextField!
218
+
219
+
220
+
221
+ // Rowを追加する(アニメーションはご自由に)
222
+
223
+ @IBAction func addRow(_ sender: UIButton) {
224
+
225
+ //func addRow() {
226
+
227
+
228
+
229
+ if dataArrayGroup.count == 0 {
230
+
231
+ return
232
+
233
+ }
234
+
235
+
236
+
237
+ let alert = UIAlertController(title:"Row追加",
238
+
239
+ message: "個数と金額を入力してください",
240
+
241
+ preferredStyle: .alert)
242
+
243
+
244
+
245
+ let cancelAction = UIAlertAction(title: "Cancel",
246
+
247
+ style: .cancel,
248
+
249
+ handler:
250
+
251
+ { action -> Void in
252
+
253
+ print("Cancel")
254
+
255
+ })
256
+
257
+
258
+
259
+ let count = dataArrayGroup[0].count
260
+
261
+ let defaultAction = UIAlertAction(title: "OK",
262
+
263
+ style: .default,
264
+
265
+ handler:
266
+
267
+ { action -> Void in
268
+
269
+
270
+
271
+ // TextFieldから値を取得
272
+
273
+ if let textFields = alert.textFields {
274
+
275
+
276
+
277
+ let data = Entity1()
278
+
279
+
280
+
281
+ for textField in textFields {
282
+
283
+
284
+
285
+ if let text = textField.text, !text.isEmpty, let _ = Int(text) {
286
+
287
+ if textField.tag == 1 {
288
+
289
+ data.count = text
290
+
291
+ } else {
292
+
293
+ data.price = text
294
+
295
+ }
296
+
297
+ }
298
+
299
+ }
300
+
301
+
302
+
303
+ self.dataArrayGroup[0].insert(data, at: count)
304
+
305
+ self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic)
306
+
307
+ }
308
+
309
+ })
310
+
311
+
312
+
313
+
314
+
315
+ alert.addTextField {
316
+
317
+ $0.placeholder = "個数"
318
+
319
+ $0.keyboardType = .numberPad
320
+
321
+ $0.tag = 1
322
+
323
+ }
324
+
325
+ alert.addTextField {
326
+
327
+ $0.placeholder = "金額"
328
+
329
+ $0.keyboardType = .numberPad
330
+
331
+ $0.tag = 2
332
+
333
+ }
334
+
335
+
336
+
337
+ alert.addAction(cancelAction)
338
+
339
+ alert.addAction(defaultAction)
340
+
341
+ present(alert, animated: true, completion: nil)
342
+
343
+ }
344
+
345
+
346
+
347
+
348
+
349
+
350
+
3
- /Users/nishikawakeien/Desktop/teratail_61308-masterのコピー2
351
+ // MARK: - TableView Delegate & DataSource
352
+
353
+ //この部分です。
354
+
355
+ func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
356
+
357
+ if sectionTitleArray.count == 0 {
358
+
359
+ return nil
360
+
361
+ } else {
362
+
363
+ return sectionTitleArray[Entity]
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ // Section Count
372
+
373
+ func numberOfSections(in tableView: UITableView) -> Int {
374
+
375
+ return dataArrayGroup.count
376
+
377
+ }
378
+
379
+
380
+
381
+ // Row Count
382
+
383
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
384
+
385
+ return dataArrayGroup[section].count
386
+
387
+ }
388
+
389
+
390
+
391
+ // Generate Cell
392
+
393
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
394
+
395
+ let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
396
+
397
+ cell.data = dataArrayGroup[indexPath.section][indexPath.row]
398
+
399
+ cell.indexLabel.text = String(indexPath.row + 1)
400
+
401
+ return cell
402
+
403
+ }
404
+
405
+
406
+
407
+ // Select Cell
408
+
409
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
410
+
411
+ tableView.deselectRow(at: indexPath as IndexPath, animated: true)
412
+
413
+ }
414
+
415
+
416
+
417
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
418
+
419
+ if editingStyle == UITableViewCellEditingStyle.delete {
420
+
421
+ dataArrayGroup[indexPath.section].remove(at: indexPath.row)
422
+
423
+ tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
424
+
425
+ }
426
+
427
+ }
428
+
429
+
430
+
431
+ //cellが削除が可能なことを伝える
432
+
433
+ func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
434
+
435
+ return UITableViewCellEditingStyle.delete;
436
+
437
+ }
438
+
439
+ }
440
+
441
+
442
+
443
+ ```
444
+
445
+ ```swift
446
+
447
+ //DataModel.swift
448
+
449
+ import CoreData
450
+
451
+ import Foundation
452
+
453
+
454
+
455
+ class Entity1 {
456
+
457
+
458
+
459
+ var count: String = "0"
460
+
461
+ var price: String = "0"
462
+
463
+ }
464
+
465
+ ```

1

書き間違え

2017/02/06 04:05

投稿

blakekei
blakekei

スコア35

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,3 @@
1
1
  永続的にデータを保存したくcoredataを書き加えたのですがエラーが起きしまいどこが間違っているのか分からないのです
2
2
 
3
- /Users/nishikawakeien/Desktop/teratail_61308-masterのコピー2.zip
3
+ /Users/nishikawakeien/Desktop/teratail_61308-masterのコピー2