Museer Ahamad Ansari
Museer Ahamad Ansari

Reputation: 5523

How to reload collection view inside UICollectionReusableView in swift

  1. I have a UICollectionView (let name aCollection) inside a View controller and i use a UICollectionReusableView to show header at the top of collection view

    enter image description here

  2. I also have a another UICollectionView (let name bCollection) in UICollectionReusableView. I need to show here top user list. but when i am trying to connect an outlet from storyboard i am getting an error

    enter image description here

  3. I know how to reload data in collection view

    self.aCollection.reloadData()

  4. My issue is how to connect bCollection outlet and how to reload bCollection to show user list coming from web services?

Upvotes: 0

Views: 2599

Answers (2)

PGDev
PGDev

Reputation: 24341

To take the outlet of bCollection, you need to create a subclass of UICollectionReusableView.

Example:

UIViewController containing aCollection:

class ViewController: UIViewController, UICollectionViewDataSource
{
    @IBOutlet weak var aCollectionView: UICollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "aCell", for: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
    {
        return (collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "reusableView", for: indexPath) as! ReusableView)
    }
}

UICollectionReusableView containing bCollection:

class ReusableView: UICollectionReusableView, UICollectionViewDataSource
{
    @IBOutlet weak var bCollectionView: UICollectionView!

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "bCell", for: indexPath)
    }
}

Interface Screenshot

enter image description here

Edit:

To reload bCollection:

You need a reference to the reusableView that you are using. The way you are using it ReusableView() is not right.

Use it like this:

var reusableView: ReusableView?

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
{
    self.reusableView = (collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "reusableView", for: indexPath) as! ReusableView) //Storing the reference to reusableView
    return self.reusableView!
}

Now, to reload bCollection,

    self.reusableView?.bCollectionView.reloadData()

Upvotes: 2

Aravind A R
Aravind A R

Reputation: 2714

Since your UICollectionView (bCollection) is inside the UICollectionReusableView you can connect outlet to UICollectionReusableView's class only. So I believe you may have to create a custom class for UICollectionReusableView and assign it in the storyboard and connect bCollection 's outlet to that custom class

Upvotes: 0

Related Questions