Snippets Collections
var arrData = [String]() // This is your data array
var arrSelectedIndex = [IndexPath]() // This is selected cell Index array
var selectedSkills = [String]() // This is selected cell data array

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
          if selectedSkills.contains(arrData[indexPath.row]) {
            cell.skillsView.backgroundColor = cell.skillsView.hexStringToUIColor(hex: "FEDA22")
        }
      return cell
    }
  
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
        
            if let cell = collectionView.cellForItem(at: indexPath) as? SkillsCollectionViewCell {

                let selectedCategory = subCategoriesPopUpArray[indexPath.item]

                if selectedSubCategories.contains(selectedCategory) {
                    cell.skillsView.backgroundColor = cell.skillsView.hexStringToUIColor(hex: "F0F0F0")
                    selectedSubCategories = selectedSubCategories.filter {
                        $0 != selectedCategory
                    }
                } else {
                    selectedSubCategories.append(selectedCategory)
                    cell.skillsView.backgroundColor = cell.skillsView.hexStringToUIColor(hex: "FEDA22")
                }
        }
    }
}
  override func viewDidLoad() {
        super.viewDidLoad()
    
           let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            let width = UIScreen.main.bounds.width
            layout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
            layout.itemSize = CGSize(width: width / 2 - 20, height: width / 2)
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            popularServicesCollectionView!.collectionViewLayout = layout
  }

func getDataFromFireStore() {
            print("getting data")
            let activityView = activityIndicatorView()
            activityView.startAnimating()
            let db = Firestore.firestore()
            db.collection("Groups").getDocuments() { (querySnapshot, error) in
                if error != nil {
                    print("Error getting documents: \(error!)")
                } else {
                    self.mentalHealthArray.removeAll()
                    self.circumstanceArray.removeAll()
                    self.identityArray.removeAll()
                    for document in querySnapshot!.documents {
                        print("for loop")
                        let group = JoinGroupModel()
                        let data = document.data()
                            group.name = data["name"] as! String
                            group.description = data["description"] as! String
                            group.image_url = data["image_url"] as! String
                        let groupType = data["type"] as! String
                        print("Group Type = \(groupType)")
                    
                        if groupType == "Mental Health" {
                            self.mentalHealthArray.append(group)
                        } else if groupType == "Circumstance" {
                            self.circumstanceArray.append(group)
                        } else {
                            self.identityArray.append(group)
                        }
                    }
                    self.mentalHealthCollectionView.reloadData()
                    self.circumstanceCollectionView.reloadData()
                    self.identityCollectionView.reloadData()
                    activityView.stopAnimating()
                }
            }
        }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if collectionView.tag == 1 {
            let cell = mentalHealthCollectionView.dequeueReusableCell(withReuseIdentifier: "MentalHealthCollectionViewCell", for: indexPath) as! MentalHealthCollectionViewCell
            cell.groupNameLabel.text = mentalHealthArray[indexPath.row].name
           let imageUrl = mentalHealthArray[indexPath.row].image_url
            cell.groupImageView.sd_setImage(with: URL(string: imageUrl.replacingOccurrences(of: " ", with: "%20")))
            cell.cellButton.tag = indexPath.row
            cell.cellButton.addTarget(self, action: #selector(metalHealthCellPressed(sender:)), for: UIControl.Event.touchUpInside)
                return cell
        } else if collectionView.tag == 2 {
            let cell = circumstanceCollectionView.dequeueReusableCell(withReuseIdentifier: "CircumstancesCollectionViewCell", for: indexPath) as! CircumstancesCollectionViewCell
            cell.groupNameLabel.text = circumstanceArray[indexPath.row].name
            let imageUrl = circumstanceArray[indexPath.row].image_url
            cell.groupImageView.sd_setImage(with: URL(string: imageUrl.replacingOccurrences(of: " ", with: "%20")))
            cell.cellButton.tag = indexPath.row
            cell.cellButton.addTarget(self, action: #selector(circumstanceCellPressed(sender:)), for: UIControl.Event.touchUpInside)
            return cell
        }
        else {
            let cell = identityCollectionView.dequeueReusableCell(withReuseIdentifier: "IdentityCollectionViewCell", for: indexPath) as! IdentityCollectionViewCell
            cell.groupNameLabel.text = identityArray[indexPath.row].name
            let imageUrl = identityArray[indexPath.row].image_url
            cell.groupImageView.sd_setImage(with: URL(string: imageUrl.replacingOccurrences(of: " ", with: "%20")))
            cell.cellButton.tag = indexPath.row
            cell.cellButton.addTarget(self, action: #selector(identityCellPressed(sender:)), for: UIControl.Event.touchUpInside)
            return cell
        }
    }

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension