Search code examples
iosswiftuicollectionviewuicollectionviewcell

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!


Solution

  • 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