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
このページでは、あるクラスから他のクラスにあるcollection viewを任意のタイミングでリロードする方法について紹介します。
例えば、ポップアップ画面などで、DBの値を更新した際に元画面(元のクラス)にあるcollection viewを更新したいというようなシーンで使えます。
実装方法
NSNotificationCenterを使うことで、任意のタイミングで、別のクラスにあるCollection Viewをリロードすることができます。
●Collection Viewの更新を通知したいクラス
// "reload"という名前の通知を送信
NotificationCenter.default.post(name: NSNotification.Name("reload"), object: nil)
●Collection Viewがあるクラス
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// 受信先の登録("reload"という名前の通知を受け取ると、reloadCollectionView()を呼び出す)
NotificationCenter.default.addObserver(self, selector: #selector(reloadCollectionView(notification:)), name: NSNotification.Name(rawValue: "reload"), object: nil)
}
// コレクションビューの更新(通知された時に呼ばれるメソッド)
@objc func reloadCollectionView(notification: NSNotification) {
self.collectionView.reloadData()
}
}



