Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload collection view data from another view class

I have two containers in a view. The top one has a collection view. I want to update my collection view from a button when a button is hit from the below container. My button is also changing the value of an array, which my collection view uses.

I thought didSet would do the job but unfortunately did 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:[])
like image 434
Koray Birand Avatar asked Apr 26 '15 14:04

Koray Birand


People also ask

How do I refresh UICollectionView?

Since iOS 10, the UITableView and UICollectionView classes have a refreshControl property. You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property.

What is swift UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.


2 Answers

I have added

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

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

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

and for the other class where the button is pressed:

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)
like image 117
Koray Birand Avatar answered Oct 04 '22 16:10

Koray Birand


Swift 4:

1st class:

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

Class with collectionView:

in viewDidLoad():

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

and function:

@objc func loadList(notification: NSNotification) {
  self.collectionView.reloadData()
}
like image 30
Bogdan Bystritskiy Avatar answered Oct 04 '22 16:10

Bogdan Bystritskiy