質問編集履歴

3

書式の改善

2019/03/21 08:37

投稿

hik_
hik_

スコア42

test CHANGED
File without changes
test CHANGED
@@ -282,6 +282,10 @@
282
282
 
283
283
  ### 追記 3月21日
284
284
 
285
+ 現状のコードです。
286
+
287
+ ご教示頂けると幸いです。
288
+
285
289
  ```ここに言語を入力
286
290
 
287
291
  import UIKit

2

情報の追加

2019/03/21 08:37

投稿

hik_
hik_

スコア42

test CHANGED
File without changes
test CHANGED
@@ -278,6 +278,284 @@
278
278
 
279
279
  ```
280
280
 
281
+
282
+
283
+ ### 追記 3月21日
284
+
285
+ ```ここに言語を入力
286
+
287
+ import UIKit
288
+
289
+
290
+
291
+ // memoTextFieldに入力された文字を格納する変数
292
+
293
+ var memoText = [String]()
294
+
295
+
296
+
297
+ // 再編集の場合の記事番号、-1なら新規作成
298
+
299
+ var honIndex = -1
300
+
301
+ var honRow: Int = honIndex
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+ class ViewController: UIViewController {
310
+
311
+
312
+
313
+ // 変数を作成
314
+
315
+ var aDate: String = ""
316
+
317
+
318
+
319
+ @IBOutlet weak var memoTextView: UITextView!
320
+
321
+
322
+
323
+
324
+
325
+ override func viewDidLoad() {
326
+
327
+ super.viewDidLoad()
328
+
329
+
330
+
331
+ // memoTextViewにaDateを入れる
332
+
333
+ memoTextView.text = aDate
334
+
335
+ }
336
+
337
+
338
+
339
+
340
+
341
+ @IBAction func setButton(_ sender: Any) {
342
+
343
+
344
+
345
+ if honRow == honIndex {
346
+
347
+ memoText.append(memoTextView.text!)
348
+
349
+ } else {
350
+
351
+ memoText[honRow] = memoTextView.text!
352
+
353
+ }
354
+
355
+ honRow = honIndex
356
+
357
+
358
+
359
+
360
+
361
+ // memoTextにmemoTextViewに入力された文字を格納
362
+
363
+ memoText.append(memoTextView.text!)
364
+
365
+
366
+
367
+
368
+
369
+ // memoTextをUserDefaultsに保存
370
+
371
+ UserDefaults.standard.set(memoText, forKey: "memoTextKey")
372
+
373
+
374
+
375
+ }
376
+
377
+
378
+
379
+ }
380
+
381
+
382
+
383
+ ```
384
+
385
+
386
+
387
+ ```ここに言語を入力
388
+
389
+ import UIKit
390
+
391
+
392
+
393
+ class TableViewController: UITableViewController {
394
+
395
+
396
+
397
+ // 変数
398
+
399
+ var honMemo: String = ""
400
+
401
+
402
+
403
+
404
+
405
+ override func viewDidLoad() {
406
+
407
+ super.viewDidLoad()
408
+
409
+
410
+
411
+
412
+
413
+ if UserDefaults.standard.object(forKey: "memoTextKey") != nil {
414
+
415
+ memoText = UserDefaults.standard.object(forKey: "memoTextKey") as! [String]
416
+
417
+ }
418
+
419
+
420
+
421
+
422
+
423
+ }
424
+
425
+
426
+
427
+ override func viewDidAppear(_ animated: Bool) {
428
+
429
+
430
+
431
+ // TableViewを開く度にtableViewを更新する
432
+
433
+ tableView.reloadData()
434
+
435
+ }
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
446
+
447
+
448
+
449
+ // memoTextの数だけCell作成
450
+
451
+ return memoText.count
452
+
453
+ }
454
+
455
+
456
+
457
+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
458
+
459
+
460
+
461
+ // Cellの表示内容を設定
462
+
463
+ let Cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+ Cell.textLabel!.text = memoText[indexPath.row]
472
+
473
+
474
+
475
+ return Cell
476
+
477
+ }
478
+
479
+
480
+
481
+ // Cellを押した時の処理
482
+
483
+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
484
+
485
+
486
+
487
+
488
+
489
+ honMemo = memoText[indexPath.item]
490
+
491
+ performSegue(withIdentifier: "Segue", sender: nil)
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+ }
502
+
503
+
504
+
505
+
506
+
507
+
508
+
509
+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
510
+
511
+ // 削除機能
512
+
513
+ let index = indexPath.row
514
+
515
+
516
+
517
+ memoText.remove(at: index)
518
+
519
+
520
+
521
+ // 削除した事を保存
522
+
523
+ UserDefaults.standard.set(memoText, forKey: "memoTextKey")
524
+
525
+ // tableViewを再読み込み
526
+
527
+ tableView.reloadData()
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+ }
536
+
537
+
538
+
539
+
540
+
541
+ // ViewControllerへの引き継ぎ設定
542
+
543
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
544
+
545
+ let vc = segue.destination as! ViewController
546
+
547
+ vc.aDate = honMemo
548
+
549
+ }
550
+
551
+
552
+
553
+
554
+
555
+ }
556
+
557
+ ```
558
+
281
559
  ### 試したこと
282
560
 
283
561
  現在TableViewCellなど解決出来そうな情報を探しているのですが、実装出来ていない状況です。

1

情報の追加(画像)、タイトルの改善

2019/03/21 08:36

投稿

hik_
hik_

スコア42

test CHANGED
@@ -1 +1 @@
1
- TableViewのCellの編集
1
+ TableViewのCellが2個表示されてしまう
test CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
 
8
8
 
9
+ ![イメージ説明](5711cd78d464e67176134fc8e46aaefc.png)
10
+
9
11
 
10
12
 
11
13
  ### 該当のソースコード