Reputation: 91
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
.
Thanks!
Upvotes: 0
Views: 323
Reputation: 91
I found an easier alternative.
Declare a function setData() in the ChildCollectionView file.
func setData(dataString: String){
print(dataString, "data you wanted to pass")
}
Take an outlet of the ChildCollectionView from the storyboard into the ParentCollectionViewCell
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)
}
}
And voila! You will now have the data available in the desired ChildCollectionView. Hope this helps someone
Upvotes: 0
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
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