質問編集履歴
5
coredetaからデータを取り出しメンバ変数に代入しtavleViewに表示させています。
test
CHANGED
File without changes
|
test
CHANGED
@@ -218,7 +218,91 @@
|
|
218
218
|
|
219
219
|
|
220
220
|
|
221
|
-
|
221
|
+
func read(){
|
222
|
+
|
223
|
+
// AooDelegateを使う用意をしておく
|
224
|
+
|
225
|
+
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
226
|
+
|
227
|
+
// エンティティを操作するためのオブジェクトを作成
|
228
|
+
|
229
|
+
let viewContext = appDelegate.persistentContainer.viewContext
|
230
|
+
|
231
|
+
// どのエンティティからデータを取得してくるか設定
|
232
|
+
|
233
|
+
let query:NSFetchRequest<ToDoGoal> = ToDoGoal.fetchRequest()
|
234
|
+
|
235
|
+
do{
|
236
|
+
|
237
|
+
// データの一括取得
|
238
|
+
|
239
|
+
let fetchResults = try viewContext.fetch(query)
|
240
|
+
|
241
|
+
// ループで一行ずつ表示
|
242
|
+
|
243
|
+
for result : AnyObject in fetchResults {
|
244
|
+
|
245
|
+
// 一行ずつのデータを取得する
|
246
|
+
|
247
|
+
// MARK:todoTextのアペンド
|
248
|
+
|
249
|
+
var todoText:String = ""
|
250
|
+
|
251
|
+
if result.value(forKey: "todoText") == nil {
|
252
|
+
|
253
|
+
print("ToDoなし")
|
254
|
+
|
255
|
+
}else{
|
256
|
+
|
257
|
+
todoText = result.value(forKey: "todoText") as! String
|
258
|
+
|
259
|
+
todoTask.append(todoText)
|
260
|
+
|
261
|
+
print("todoTextは:\(todoText)")
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// MARK:goalTextのアペンド
|
268
|
+
|
269
|
+
var goalText:String = ""
|
270
|
+
|
271
|
+
var goaletail:String = ""//追加
|
272
|
+
|
273
|
+
if result.value(forKey: "goalTitle") == nil || result.value(forKey: "goalDetail") == nil {
|
274
|
+
|
275
|
+
print("goalTextなし")
|
276
|
+
|
277
|
+
print("goalDetailなし")
|
278
|
+
|
279
|
+
}else{
|
280
|
+
|
281
|
+
goalText = result.value(forKey: "goalTitle") as! String
|
282
|
+
|
283
|
+
goaletail = result.value(forKey: "goalDetail") as! String//追加
|
284
|
+
|
285
|
+
print("goalTitle:は\(goalText)")
|
286
|
+
|
287
|
+
print("goaletail:は\(goaletail)")//追加
|
288
|
+
|
289
|
+
goalTask.append(goalText)
|
290
|
+
|
291
|
+
goalTaskDetail.append(goaletail)//追加
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
}catch{
|
298
|
+
|
299
|
+
// エラーが起きた時に通常処理の代わりに行う処理を記述(例外処理を記述する場合)
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
222
306
|
|
223
307
|
override func viewWillAppear(_ animated: Bool) {
|
224
308
|
|
4
goalでは下記のような記述となっております。
test
CHANGED
File without changes
|
test
CHANGED
@@ -268,7 +268,185 @@
|
|
268
268
|
|
269
269
|
```
|
270
270
|
|
271
|
-
|
271
|
+
```class GoalViewController: UIViewController,UITextFieldDelegate {
|
272
|
+
|
273
|
+
@IBOutlet weak var textField: UITextField!
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
@IBOutlet weak var textView: UITextView!
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
@IBAction func addBtn(_ sender: UIButton) {
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
let taskName = textField.text
|
290
|
+
|
291
|
+
let task = textView.text
|
292
|
+
|
293
|
+
// let taskView = textView.text
|
294
|
+
|
295
|
+
// 条件追加でから文字を指定できる
|
296
|
+
|
297
|
+
// 画面戻ります
|
298
|
+
|
299
|
+
print("文字が入力された")
|
300
|
+
|
301
|
+
let storyboard: UIStoryboard = self.storyboard!
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
self.dismiss(animated: true, completion: nil)
|
306
|
+
|
307
|
+
// context(データベースを扱うのに必要)を定義。
|
308
|
+
|
309
|
+
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
310
|
+
|
311
|
+
// (データベースのエンティティです)型オブジェクトを代入します。
|
312
|
+
|
313
|
+
let todogoal = ToDoGoal (context: context)
|
314
|
+
|
315
|
+
// 先ほど定義したTask型データのname、categoryプロパティに入力、選択したデータを代入します。
|
316
|
+
|
317
|
+
if taskName == "" || taskName == nil || task == "" || task == nil {
|
318
|
+
|
319
|
+
print("文字が入力されていない")
|
320
|
+
|
321
|
+
// textLabel.text = "文字が入力されていません"
|
322
|
+
|
323
|
+
}else{
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
todogoal.goalTitle = taskName!
|
328
|
+
|
329
|
+
todogoal.goalDetail = task!
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
print("タイトル:\(todogoal.goalTitle!)")
|
334
|
+
|
335
|
+
print("詳細\(todogoal.goalDetail!)")
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
// 上で作成したデータをデータベースに保存します。
|
342
|
+
|
343
|
+
(UIApplication.shared.delegate as! AppDelegate).saveContext()
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
// 戻るボタン
|
354
|
+
|
355
|
+
@IBAction func back(_ sender: UIButton) {
|
356
|
+
|
357
|
+
self.dismiss(animated: true, completion: nil)
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
@IBAction func textFieldBack(_ sender: UITextField) {
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
func makeKeybord(){
|
370
|
+
|
371
|
+
// 仮のサイズでツールバー生成
|
372
|
+
|
373
|
+
let kbToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
|
374
|
+
|
375
|
+
kbToolBar.barStyle = UIBarStyle.default // スタイルを設定
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
kbToolBar.sizeToFit() // 画面幅に合わせてサイズを変更
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
// スペーサー
|
384
|
+
|
385
|
+
let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
// 閉じるボタン
|
390
|
+
|
391
|
+
let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(GoalViewController.commitButtonTapped))
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
kbToolBar.items = [spacer, commitButton]
|
396
|
+
|
397
|
+
textView.inputAccessoryView = kbToolBar
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
func commitButtonTapped (){
|
404
|
+
|
405
|
+
self.view.endEditing(true)
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
override func viewDidLoad() {
|
420
|
+
|
421
|
+
super.viewDidLoad()
|
422
|
+
|
423
|
+
makeKeybord()
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
override func didReceiveMemoryWarning() {
|
434
|
+
|
435
|
+
super.didReceiveMemoryWarning()
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
```
|
272
450
|
|
273
451
|
|
274
452
|
|
3
テーブルがダブって表示されます。
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,19 +24,7 @@
|
|
24
24
|
|
25
25
|
```Swift3
|
26
26
|
|
27
|
-
|
27
|
+
|
28
|
-
|
29
|
-
// ToDo&GoalViewController.swift
|
30
|
-
|
31
|
-
// goodGlife
|
32
|
-
|
33
|
-
//
|
34
|
-
|
35
|
-
// Created by togashi yoshiki on 2017/06/29.
|
36
|
-
|
37
|
-
// Copyright © 2017年 Yoshiki Togashi. All rights reserved.
|
38
|
-
|
39
|
-
//
|
40
28
|
|
41
29
|
|
42
30
|
|
@@ -68,25 +56,131 @@
|
|
68
56
|
|
69
57
|
@IBOutlet weak var subTableView: UITableView!
|
70
58
|
|
59
|
+
|
60
|
+
|
71
|
-
|
61
|
+
@IBAction func addToDo(_ sender: UIButton) {
|
62
|
+
|
72
|
-
|
63
|
+
// テキストフィールド付きアラート表示
|
64
|
+
|
65
|
+
let alert = UIAlertController(title: "ToDo", message: "文字を入力してください。", preferredStyle: .alert)
|
66
|
+
|
67
|
+
// OKボタンの設定
|
68
|
+
|
69
|
+
let okAction = UIAlertAction(title: "OK", style: .default, handler: {
|
70
|
+
|
71
|
+
(action:UIAlertAction!) -> Void in
|
72
|
+
|
73
|
+
// OKを押した時入力されていたテキストを表示
|
74
|
+
|
75
|
+
if let textFields = alert.textFields {
|
76
|
+
|
77
|
+
// アラートに含まれるすべてのテキストフィールドを調べる
|
78
|
+
|
79
|
+
for textField in textFields {
|
80
|
+
|
81
|
+
// AppDelegateのインスタンスを用意しておく
|
82
|
+
|
83
|
+
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
84
|
+
|
85
|
+
// エンティティを操作するためのオブジェクト
|
86
|
+
|
87
|
+
let viewContext = appDelegate.persistentContainer.viewContext
|
88
|
+
|
89
|
+
// ToDoエンティティオブジェクトを作成
|
90
|
+
|
91
|
+
let ToDoGoal = NSEntityDescription.entity(forEntityName: "ToDoGoal", in: viewContext)
|
92
|
+
|
93
|
+
// ToDoエンティティにレコード(行)を挿入するためのオブジェクトを作成
|
94
|
+
|
95
|
+
let newRecord = NSManagedObject(entity: ToDoGoal!, insertInto: viewContext)
|
96
|
+
|
97
|
+
// 追加したいデータ(txtTitleに入力された文字)のセット
|
98
|
+
|
99
|
+
if textField.text! == "" || textField.text! == nil{
|
100
|
+
|
101
|
+
print("nilが入っています。")
|
102
|
+
|
103
|
+
}else{
|
104
|
+
|
105
|
+
newRecord.setValue(textField.text!, forKey: "todoText")
|
106
|
+
|
107
|
+
// newRecord.setValue(Date(), forKey: "todoDeta")
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// レコード(行)の即時保存
|
112
|
+
|
113
|
+
do{
|
114
|
+
|
115
|
+
try viewContext.save()
|
116
|
+
|
117
|
+
}catch{
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
print("右の文字が入る\(textField.text!)")
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
self.todoTask.append(textField.text!)
|
126
|
+
|
127
|
+
self.TableView.reloadData()
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
})
|
138
|
+
|
139
|
+
alert.addAction(okAction)
|
140
|
+
|
141
|
+
// キャンセルボタンの設定
|
142
|
+
|
143
|
+
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
144
|
+
|
145
|
+
alert.addAction(cancelAction)
|
146
|
+
|
147
|
+
// テキストフィールドを追加
|
148
|
+
|
149
|
+
alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
|
150
|
+
|
151
|
+
textField.placeholder = "テキスト"
|
152
|
+
|
153
|
+
})
|
154
|
+
|
155
|
+
alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
|
156
|
+
|
157
|
+
// アラートを画面に表示
|
158
|
+
|
159
|
+
self.present(alert, animated: true, completion: nil)
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
73
|
-
//
|
167
|
+
// ⑵行数を決める
|
74
|
-
|
75
|
-
|
168
|
+
|
76
|
-
|
77
|
-
|
169
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
78
|
-
|
170
|
+
|
79
|
-
if
|
171
|
+
if tableView.tag == 1{
|
80
|
-
|
172
|
+
|
81
|
-
|
173
|
+
print(tableView.tag)
|
82
|
-
|
174
|
+
|
83
|
-
|
175
|
+
return todoTask.count
|
84
176
|
|
85
177
|
}else{
|
86
178
|
|
87
|
-
|
179
|
+
print(tableView.tag)
|
180
|
+
|
88
|
-
|
181
|
+
// return goalTask.count
|
182
|
+
|
89
|
-
|
183
|
+
return goalTaskDetail.count
|
90
184
|
|
91
185
|
}
|
92
186
|
|
@@ -94,129 +188,29 @@
|
|
94
188
|
|
95
189
|
|
96
190
|
|
97
|
-
@IBAction func addToDo(_ sender: UIButton) {
|
98
|
-
|
99
|
-
|
191
|
+
// ⑶リストに表示する文字列を決定し、表示
|
100
|
-
|
101
|
-
|
192
|
+
|
102
|
-
|
103
|
-
// OKボタンの設定
|
104
|
-
|
105
|
-
let okAction = UIAlertAction(title: "OK", style: .default, handler: {
|
106
|
-
|
107
|
-
(action:UIAlertAction!) -> Void in
|
108
|
-
|
109
|
-
// OKを押した時入力されていたテキストを表示
|
110
|
-
|
111
|
-
if let textFields = alert.textFields {
|
112
|
-
|
113
|
-
// アラートに含まれるすべてのテキストフィールドを調べる
|
114
|
-
|
115
|
-
for textField in textFields {
|
116
|
-
|
117
|
-
// AppDelegateのインスタンスを用意しておく
|
118
|
-
|
119
|
-
|
193
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {
|
120
|
-
|
194
|
+
|
121
|
-
|
195
|
+
// 文字を表示するセルの取得(セルの再利用)
|
122
|
-
|
123
|
-
|
196
|
+
|
124
|
-
|
125
|
-
// ToDoエンティティオブジェクトを作成
|
126
|
-
|
127
|
-
|
197
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
|
128
|
-
|
129
|
-
|
198
|
+
|
130
|
-
|
131
|
-
let newRecord = NSManagedObject(entity: ToDoGoal!, insertInto: viewContext)
|
132
|
-
|
133
|
-
// 追加したいデータ(txtTitleに入力された文字)のセット
|
134
|
-
|
135
|
-
if textField.text! == "" || textField.text! == nil{
|
136
|
-
|
137
|
-
print("nilが入っています。")
|
138
|
-
|
139
|
-
}else{
|
140
|
-
|
141
|
-
newRecord.setValue(textField.text!, forKey: "todoText")
|
142
|
-
|
143
|
-
// newRecord.setValue(Date(), forKey: "todoDeta")
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
199
|
+
// 表示したい文字の設定
|
148
|
-
|
149
|
-
do{
|
150
|
-
|
151
|
-
try viewContext.save()
|
152
|
-
|
153
|
-
}catch{
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
print("右の文字が入る\(textField.text!)")
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
self.todoTask.append(textField.text!)
|
162
|
-
|
163
|
-
self.TableView.reloadData()
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
}
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
})
|
174
|
-
|
175
|
-
alert.addAction(okAction)
|
176
|
-
|
177
|
-
// キャンセルボタンの設定
|
178
|
-
|
179
|
-
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
180
|
-
|
181
|
-
alert.addAction(cancelAction)
|
182
|
-
|
183
|
-
// テキストフィールドを追加
|
184
|
-
|
185
|
-
alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
|
186
|
-
|
187
|
-
textField.placeholder = "テキスト"
|
188
|
-
|
189
|
-
})
|
190
|
-
|
191
|
-
alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
|
192
|
-
|
193
|
-
// アラートを画面に表示
|
194
|
-
|
195
|
-
self.present(alert, animated: true, completion: nil)
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
// ⑵行数を決める
|
204
|
-
|
205
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
206
200
|
|
207
201
|
if tableView.tag == 1{
|
208
202
|
|
209
|
-
|
203
|
+
cell.textLabel?.text=todoTask[indexPath.row]
|
210
|
-
|
204
|
+
|
211
|
-
return
|
205
|
+
return cell
|
212
206
|
|
213
207
|
}else{
|
214
208
|
|
215
|
-
print(tableView.tag)
|
216
|
-
|
217
|
-
// return goalTask.count
|
218
|
-
|
219
|
-
|
209
|
+
cell.goalDetail?.text=goalTaskDetail[indexPath.row]
|
210
|
+
|
211
|
+
cell.goalTitle?.text=goalTask[indexPath.row]
|
212
|
+
|
213
|
+
return cell
|
220
214
|
|
221
215
|
}
|
222
216
|
|
@@ -224,226 +218,40 @@
|
|
224
218
|
|
225
219
|
|
226
220
|
|
221
|
+
|
222
|
+
|
223
|
+
override func viewWillAppear(_ animated: Bool) {
|
224
|
+
|
227
|
-
|
225
|
+
// CoreDataからデータをfetchしてくる
|
228
|
-
|
226
|
+
|
229
|
-
|
227
|
+
read()
|
230
|
-
|
228
|
+
|
231
|
-
//
|
229
|
+
// self.TableView.reloadData()
|
232
|
-
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
-
|
233
|
+
self.subTableView.reloadData()
|
234
|
+
|
234
|
-
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
235
|
-
|
241
|
+
// MARK:初期設定メソッド
|
236
|
-
|
242
|
+
|
237
|
-
|
243
|
+
override func viewDidLoad() {
|
238
|
-
|
239
|
-
|
244
|
+
|
240
|
-
|
241
|
-
|
245
|
+
super.viewDidLoad()
|
242
|
-
|
243
|
-
|
246
|
+
|
244
|
-
|
245
|
-
|
247
|
+
|
246
|
-
|
247
|
-
|
248
|
+
|
248
|
-
|
249
|
-
|
249
|
+
|
250
|
+
|
251
|
+
|
250
252
|
|
251
253
|
}
|
252
254
|
|
253
|
-
}
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
// 削除機能
|
262
|
-
|
263
|
-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
|
264
|
-
|
265
|
-
if editingStyle == .delete {
|
266
|
-
|
267
|
-
todoTask.remove(at: indexPath.row)
|
268
|
-
|
269
|
-
print(todoTask)
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
// MARK:削除方法がよく分からない
|
274
|
-
|
275
|
-
// goalTask.remove(at: indexPath.row)
|
276
|
-
|
277
|
-
// goalTaskDetail.remove(at: indexPath.row)
|
278
|
-
|
279
|
-
print(goalTaskDetail)
|
280
|
-
|
281
|
-
tableView.deleteRows(at: [indexPath], with: .fade)
|
282
|
-
|
283
|
-
// AooDelegateを使う用意をしておく
|
284
|
-
|
285
|
-
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
286
|
-
|
287
|
-
// エンティティを操作するためのオブジェクトを作成
|
288
|
-
|
289
|
-
let viewContext = appDelegate.persistentContainer.viewContext
|
290
|
-
|
291
|
-
// どのエンティティからデータを取得してくるか設定
|
292
|
-
|
293
|
-
let query:NSFetchRequest<ToDoGoal> = ToDoGoal.fetchRequest()
|
294
|
-
|
295
|
-
do{
|
296
|
-
|
297
|
-
// 削除するデータを取得
|
298
|
-
|
299
|
-
let fetchResults = try viewContext.fetch(query)
|
300
|
-
|
301
|
-
// 削除するデータを取得
|
302
|
-
|
303
|
-
for result : AnyObject in fetchResults {
|
304
|
-
|
305
|
-
// 一行ずつ削除
|
306
|
-
|
307
|
-
let record = result as! NSManagedObject
|
308
|
-
|
309
|
-
viewContext.delete(record)
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
// 削除した状態を保存(処理の確定)
|
314
|
-
|
315
|
-
try viewContext.save()
|
316
|
-
|
317
|
-
}catch{
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
func read(){
|
330
|
-
|
331
|
-
// AooDelegateを使う用意をしておく
|
332
|
-
|
333
|
-
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
334
|
-
|
335
|
-
// エンティティを操作するためのオブジェクトを作成
|
336
|
-
|
337
|
-
let viewContext = appDelegate.persistentContainer.viewContext
|
338
|
-
|
339
|
-
// どのエンティティからデータを取得してくるか設定
|
340
|
-
|
341
|
-
let query:NSFetchRequest<ToDoGoal> = ToDoGoal.fetchRequest()
|
342
|
-
|
343
|
-
do{
|
344
|
-
|
345
|
-
// データの一括取得
|
346
|
-
|
347
|
-
let fetchResults = try viewContext.fetch(query)
|
348
|
-
|
349
|
-
// ループで一行ずつ表示
|
350
|
-
|
351
|
-
for result : AnyObject in fetchResults {
|
352
|
-
|
353
|
-
// 一行ずつのデータを取得する
|
354
|
-
|
355
|
-
// MARK:todoTextのアペンド
|
356
|
-
|
357
|
-
var todoText:String = ""
|
358
|
-
|
359
|
-
if result.value(forKey: "todoText") == nil {
|
360
|
-
|
361
|
-
print("ToDoなし")
|
362
|
-
|
363
|
-
}else{
|
364
|
-
|
365
|
-
todoText = result.value(forKey: "todoText") as! String
|
366
|
-
|
367
|
-
todoTask.append(todoText)
|
368
|
-
|
369
|
-
print("todoTextは:\(todoText)")
|
370
|
-
|
371
|
-
}
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
// MARK:goalTextのアペンド
|
376
|
-
|
377
|
-
var goalText:String = ""
|
378
|
-
|
379
|
-
var goaletail:String = ""//追加
|
380
|
-
|
381
|
-
if result.value(forKey: "goalTitle") == nil || result.value(forKey: "goalDetail") == nil {
|
382
|
-
|
383
|
-
print("goalTextなし")
|
384
|
-
|
385
|
-
print("goalDetailなし")
|
386
|
-
|
387
|
-
}else{
|
388
|
-
|
389
|
-
goalText = result.value(forKey: "goalTitle") as! String
|
390
|
-
|
391
|
-
goaletail = result.value(forKey: "goalDetail") as! String//追加
|
392
|
-
|
393
|
-
print("goalTitle:は\(goalText)")
|
394
|
-
|
395
|
-
print("goaletail:は\(goaletail)")//追加
|
396
|
-
|
397
|
-
goalTask.append(goalText)
|
398
|
-
|
399
|
-
goalTaskDetail.append(goaletail)//追加
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
}catch{
|
406
|
-
|
407
|
-
// エラーが起きた時に通常処理の代わりに行う処理を記述(例外処理を記述する場合)
|
408
|
-
|
409
|
-
}
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
override func viewWillAppear(_ animated: Bool) {
|
414
|
-
|
415
|
-
// CoreDataからデータをfetchしてくる
|
416
|
-
|
417
|
-
read()
|
418
|
-
|
419
|
-
// self.TableView.reloadData()
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
self.subTableView.reloadData()
|
424
|
-
|
425
|
-
}
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
// MARK:初期設定メソッド
|
432
|
-
|
433
|
-
override func viewDidLoad() {
|
434
|
-
|
435
|
-
super.viewDidLoad()
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
mainView.isHidden = false
|
442
|
-
|
443
|
-
subView.isHidden = true
|
444
|
-
|
445
|
-
}
|
446
|
-
|
447
255
|
|
448
256
|
|
449
257
|
override func didReceiveMemoryWarning() {
|
2
アクサングラーブでコードを囲いました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,7 +18,11 @@
|
|
18
18
|
|
19
19
|
###該当のソースコード
|
20
20
|
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
21
|
-
Swift3
|
25
|
+
```Swift3
|
22
26
|
|
23
27
|
//
|
24
28
|
|
@@ -36,6 +40,8 @@
|
|
36
40
|
|
37
41
|
|
38
42
|
|
43
|
+
|
44
|
+
|
39
45
|
import UIKit
|
40
46
|
|
41
47
|
import CoreData
|
@@ -452,6 +458,14 @@
|
|
452
458
|
|
453
459
|
|
454
460
|
|
461
|
+
```
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
|
468
|
+
|
455
469
|
###試したこと
|
456
470
|
|
457
471
|
tableViewのnumberOfRowsInSectionで表示行数の変更 ×
|
1
###発生している問題・の追加処理を致しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
TableViewの文字がダブって再度TableViewに表示されてしまいます。
|
14
14
|
|
15
|
-
|
15
|
+
2度、3度行うとnilが入ってしまう為エラーになってしまいます。
|
16
16
|
|
17
17
|
|
18
18
|
|