質問編集履歴
2
Noteの定義を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
```ここに言語を入力
|
2
|
+
|
3
|
+
コード
|
4
|
+
|
1
|
-
保存ができなくて困っています。
|
5
|
+
```保存ができなくて困っています。
|
2
6
|
|
3
7
|
メモ帳アプリを作成しています。
|
4
8
|
|
@@ -416,4 +420,22 @@
|
|
416
420
|
|
417
421
|
|
418
422
|
|
423
|
+
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
```ここに言語を入力
|
428
|
+
|
429
|
+
import Foundation
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
class Note {
|
434
|
+
|
435
|
+
var title = ""
|
436
|
+
|
437
|
+
var content = ""
|
438
|
+
|
439
|
+
}
|
440
|
+
|
419
441
|
```
|
1
保存の処理を記載しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -211,3 +211,209 @@
|
|
211
211
|
}
|
212
212
|
|
213
213
|
```
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```ここに言語を入力
|
224
|
+
|
225
|
+
import UIKit
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
class NotesTableViewController: UITableViewController {
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
/*
|
234
|
+
|
235
|
+
notesプロパティが変更されるたびに、すべてのデータをリロードするように
|
236
|
+
|
237
|
+
didSetプロパティオブザーバを用いてテーブルビューに通知
|
238
|
+
|
239
|
+
*/
|
240
|
+
|
241
|
+
var notes = [Note]() {
|
242
|
+
|
243
|
+
didSet {
|
244
|
+
|
245
|
+
tableView.reloadData()
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
override func viewDidLoad() {
|
254
|
+
|
255
|
+
super.viewDidLoad()
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
// Uncomment the following line to preserve selection between presentations
|
260
|
+
|
261
|
+
// self.clearsSelectionOnViewWillAppear = false
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
|
266
|
+
|
267
|
+
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
// 表示すべきセルの数を返す
|
274
|
+
|
275
|
+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
276
|
+
|
277
|
+
return notes.count
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
// テーブルの特定の行に対して、表示すべきcellを返す
|
284
|
+
|
285
|
+
// indexPathは、作成するcellがデーブルビュー内のどのセクションと行に属するかを示す
|
286
|
+
|
287
|
+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
288
|
+
|
289
|
+
//テーブルビューに表示される実際のcellをフェッチしている
|
290
|
+
|
291
|
+
//ダウンキャスト <- NotesTableViewCellがUITableViewCellのサブクラスであるため機能する
|
292
|
+
|
293
|
+
//コンパイラに具体的な種類のUITableViewCellを返すことを指示する
|
294
|
+
|
295
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "notesTableViewCell", for: indexPath) as! NotesTableViewCell
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
// indexPathは、cellForRowに渡された引数であり、テーブルビューがセルをどの行に表示するのかを示す
|
300
|
+
|
301
|
+
// indexPathのrowプロパティにアクセスし、検索する。
|
302
|
+
|
303
|
+
let row = indexPath.row
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
let note = notes[row]
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
cell.noteTitleLabel.text = note.title
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
return cell
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
// commitEditingStyle : forRowAt indexPath: により、テーブルビューに追加の編集モードを有効にする
|
326
|
+
|
327
|
+
// cellを左スワイプした際に削除オプションが表示される
|
328
|
+
|
329
|
+
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
|
330
|
+
|
331
|
+
// editingStyleが.deleteであるかどうかを調べる
|
332
|
+
|
333
|
+
if editingStyle == .delete {
|
334
|
+
|
335
|
+
// notesPathから渡されたindexPathのrowプロパティを使用して、選択されたノートをnotes配列から削除する
|
336
|
+
|
337
|
+
notes.remove(at: indexPath.row)
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
348
|
+
|
349
|
+
if let identifier = segue.identifier {
|
350
|
+
|
351
|
+
if identifier == "displayNote" {
|
352
|
+
|
353
|
+
print("Table view cell tapped")
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
// すべてのテーブルビューは、indexPathForSelecedRowというプロパティを持っている。
|
358
|
+
|
359
|
+
// ユーザーがテーブルビューからcellを選択した場合、特定のcellのindexPathにアクセス可能
|
360
|
+
|
361
|
+
// indexPathにはsection,rowのプロパティがあり、選択したテーブルビューのcellをマッピングする
|
362
|
+
|
363
|
+
// データモデル(ここではnotes配列)に関連付けることができる。
|
364
|
+
|
365
|
+
let indexPath = tableView.indexPathForSelectedRow!
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
// テーブルビューにはsectionが1つしかないため、対応するindexPathのrowプロパティを使用して
|
370
|
+
|
371
|
+
// 各cellを一意に識別できる。
|
372
|
+
|
373
|
+
// indexPath.rowで、タッチされたcellに対応するnotes配列からノートを取り出す。
|
374
|
+
|
375
|
+
let note = notes[indexPath.row]
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
// ダウンキャスト
|
380
|
+
|
381
|
+
// segueのdestinationプロパティを使用して、DispalyNoteViewControllerにアクセス
|
382
|
+
|
383
|
+
let displayNoteViewController = segue.destination as! DisplayNoteViewController
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
// DisplayNoteViewControllerのnoteプロパティを、ユーザーがタッチしたcellに対応するnoteに設定
|
388
|
+
|
389
|
+
displayNoteViewController.note = note
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
} else if identifier == "addNote" {
|
394
|
+
|
395
|
+
print("+ button tapped")
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
@IBAction func unwindToNotesViewController(_ segue: UIStoryboardSegue) {
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
```
|