At the moment I'm parsing data from a Json and saving it local. Then I fill up a CollectionView with the data from the first Array. But now I want to add a second CollectionView that depends on the selection from the first CollectionView.
My Json structure:
struct Posts: Codable {
let id: Int
let name: String
let ImageURL: String
let Rarity: [PostType]
}
struct PostType: Codable {
let type: String
let test: int
let test2: String
}
What I tried:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.PostsCollectionView{
return getPostsFromDisk().count
}else
{
return feedWrapper.count //not working
}
}
var feedWrapper:[PostType] = []
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.PostsCollectionView{
let cell = PostsCollectionView.dequeueReusableCell(withReuseIdentifier: "PostsUICollectionViewCell", for: indexPath) as! PostsUICollectionViewCell
cell.PostsNameLbl.text = getPostsFromDisk()[indexPath.row].name
cell.PostsImage.sd_setImage(with: URL(string: getPostsFromDisk()[indexPath.row].ImageURL))
return cell
}else
{
let cell = PostsRareCollectionView.dequeueReusableCell(withReuseIdentifier: "PostsRareCollectionViewCell", for: indexPath) as! PostsRareCollectionViewCell
if(feedWrapper[indexPath.row].type == "normal"){
cell.PostsRareImageView.image = [imageFromAssets]
}
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
PostsName.text = getPostsFromDisk()[indexPath.row].name
//thougt i could save the selecte array to feedWrapper but its not working like expected
feedWrapper = getPostsFromDisk()[indexPath.row].Rarity
}
I think my var feedWrapper
isn't filled up at the beginning but I'm not sure how to do this
I hope someone can help me with this
Thanks in advance
You need to reload the collectionView. You can use this code:
Declare your variable with didset (Assumed that your array is inside view controller)
var feedWrapper:[PostType] = [] {
didSet {
self.yourCollectionView.reloadData()
}
}