Warning: Undefined array key 1 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 12
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21
Warning: Undefined array key 1 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 24
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33
次のケースのように遷移先のViewから戻ってきた時に、遷移元のViewにあるテーブルのデータを再表示する方法について紹介します。
- TableViewで構成されたViewA→ViewBへ移動
- ViewBでテーブルのデータを操作
- ViewAに戻った時に、ViewBで変更した値を反映させる。
実装方法
ViewA(遷移元)の画面は次のようになります。
// 対象のtableViewを接続
@IBOutlet weak var tableView: UITableView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//テーブルを再描画
tableView.reloadData()
}
ポイントはviewWillAppearの中でテーブルをリロードすることです。
遷移先から戻った時に、viewDidLoadの処理は走らないので、この中にreloadDataを書いても値が更新されません。
一方、viewWillAppearは、画面遷移してから戻る際にも呼ばれるため、この中にテーブルを更新して表示する処理を書いておけば最新の状態のテーブルを表示することが可能です。



