'Swift Filtering data with collectionview

So im trying to filter my collectionview data using another collectionview, something like this enter image description here

as from the picture on i try to make a filter button so i can filter the data base on the button that user tap, and the way im trying to make that filter button is to use another collectionview, but i dont know how to implement it.

so far here's my code :

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == homeColView {
  return homeVm.heroList.count
}else if collectionView == filterColView {
  return homeVm.filterRoles.count
  }
return 0
  }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == homeColView{
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as! HomeCollectionCell
  let data = homeVm.heroList[indexPath.row]
  let imgUrl = URL(string: BASE_URL + data.img)
  
  cell.heroNameLabel.text = data.localized_name
  cell.heroImage.kf.setImage(with: imgUrl)
  
  return cell
}else if collectionView == filterColView {
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterCollectionCell", for: indexPath) as! FilterCollectionCell
  
  cell.filterLabel.text = homeVm.filterRoles[indexPath.row]
  return cell
}
return collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as! HomeCollectionCell
 }

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if collectionView == homeColView {
  let vc = HeroDetailsVc()
  vc.heroDetailsVm.heroDetails = homeVm.heroList[indexPath.row]
  
  self.navigationController?.pushViewController(vc, animated: true)
  
  collectionView.deselectItem(at: indexPath as IndexPath, animated: true)
}else if collectionView == filterColView {
  
}

}

can anyone help me how to make this filter with collectionview?

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source