Samir K
Samir K

Reputation: 91

How can i retrieve data in my child UICollectionView from my parent UICollectionViewCell - swift 4

Suppose I have a ParentCollectionViewController which has the data called assetID.

This data is also available in the ParentCollectionViewControllerCell.

How can I pass this data in the ChildCollectionView which is located in each ParentCollectionViewControllerCell.

Here's the image

Thanks!

Upvotes: 0

Views: 323

Answers (3)

Samir K
Samir K

Reputation: 91

I found an easier alternative.

  1. Declare a function setData() in the ChildCollectionView file.

    func setData(dataString: String){
       print(dataString, "data you wanted to pass")
    }
    
  2. Take an outlet of the ChildCollectionView from the storyboard into the ParentCollectionViewCell

  3. Call the setData(data) function in ParentCollectionViewCell using the outlet and pass the data into it. Like this:

    var dataString: String! {
      didSet {
        childCollectionViewOutlet.setData(dataString: dataString)
      }
    }
    
  4. And voila! You will now have the data available in the desired ChildCollectionView. Hope this helps someone

Upvotes: 0

Rajat Gupta
Rajat Gupta

Reputation: 120

First of all, if you want to send the data first you need to register child collectionviewcell in ParentCollectionViewControllerCell. This you will do in awakeFromNib().

override func awakeFromNib() {
   childCollectionView.register(UINib.init(nibName: "childCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "childCollectionViewCell")
   childCollectionView.reloadData()
}

So it means you have all the delegates and datasource of child collectionview in ParentCollectionViewControllerCell. So you will send the data through cellForItemAtindexPath:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "childCollectionViewCell", for: indexPath) as! childCollectionViewCell
        cell.bindAssetid(strAssetId: assetID!)
        return cell
    }

I think this answer fixed your requirement.

Happy Coding

Upvotes: 1

Saiko Diabović
Saiko Diabović

Reputation: 15

First of all you need to detect the selected UICollectionView then you have to get the data from that cell, and do whatever with it, you can simply check that in the didSelectRowAt function and then do whatever with your data lets say for example you want to get something called foo from the sub cell and display it in the Parent cell, your code should be something like this,

 var myFooData: String?
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == parentCollectionView { // if the selected cell from the parent collection view do something

        } else {
            //if the selected cell from the sub collection view do something
        myFooData = (collectionView.cellForItem(at: indexPath) as! MyCusomCell).MyFooData
        }
    }

Upvotes: 0

Related Questions