Reloading the collection View data from another view class

I have two containers in a view. The top view is a collection view. I want to update the collection view with one button when the button from the container below is clicked. My button also changes the value of the array that my collection view uses.

I thought that didSet would do the job, but unfortunately does not work.

Top:

class TopViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var favoritesCV: UICollectionView!

    var myFavorites = [] {
        didSet {
            self.favoritesCV.reloadData()
        }
    }


    override func viewDidAppear(animated: Bool) {
        myFavorites = favoritesInstance.favoritesArray
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return myFavorites.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell : FavoritesCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FavoritesCollectionViewCell

        var myPath = myFavorites[indexPath.row] as! String
        cell.myImage.image = UIImage(named: myPath)
        return cell
    }
 }

Bottom:

class BottomViewController: UIViewController, UIScrollViewDelegate  {

    @IBAction func addFavorites(sender: AnyObject) {
         favoritesInstance.favoritesArray.append("aaaa.jpg")
    }
}

Storage class:

class Favorites {
    var favoritesArray:Array <AnyObject>

    init(favoritesArray:Array <AnyObject>) {
        self.favoritesArray = favoritesArray
    }
}

var favoritesInstance = Favorites(favoritesArray:[])
+4
source share
3 answers

I added

NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)

in my viewdidload class collection. also added a selector that reloads my data when it is called by the Notification Center

func loadList(notification: NSNotification){
    self.favoritesCV.reloadData()
}

and for another class where the button is clicked:

NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)

Swift 3:

NotificationCenter.default.addObserver(self, selector: #selector(loadList), name:NSNotification.Name(rawValue: "load"), object: nil)

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
+5
source

Swift 4:

1- :

NotificationCenter.default.post(name: NSNotification.Name("load"), object: nil)

collectionView:

in viewDidLoad():

NotificationCenter.default.addObserver(self, selector: #selector(loadList(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)

:

@objc func loadList(notification: NSNotification) {
  self.collectionView.reloadData()
}
+1

Fine! I was looking for it. In Swift 3, the code is slightly different. In the collection controller:

NotificationCenter.default.addObserver(self, selector: #selector(RoundCollectionViewController.load), name:NSNotification.Name(rawValue: "reload"), object: nil)

and in another:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
0
source

Source: https://habr.com/ru/post/1584742/


All Articles