Snippets Collections
extension ServiceProviderProfileViewController : UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return categories.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = categoryCollectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCollectionViewCell", for: indexPath) as! CategoryCollectionViewCell

            cell.categoryLabel.text = categories[indexPath.row]
        
            if indexPath.row == selectedCellIndex {
                cell.categoryView.layer.borderColor = UIColor.blue.cgColor
                cell.categoryLabel.textColor = UIColor.blue
            } else {
                cell.categoryView.layer.borderColor = UIColor.lightGray.cgColor
                cell.categoryLabel.textColor = UIColor.gray
            }
            return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedCellIndex = indexPath.row
        self.categoryCollectionView.reloadData()
    }
    
}
// install pod :: AlignedCollectionViewFlowLayout
// also assign class AlignedCollectionViewFlowLayout in storyboard

func setLayout() {

        let alignedFlowLayout = AlignedCollectionViewFlowLayout(horizontalAlignment: .justified, verticalAlignment: .center)

        statsCollectionView.collectionViewLayout = alignedFlowLayout

        statsCollectionView.delegate = self
        statsCollectionView.dataSource = self

    }
    
}

// MARK: - Collection View

extension GameStatsViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        print("sec :", section)

        return UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0)//here your custom value for spacing
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        var widthPerItem : CGFloat = 0
        var heightPerItem : CGFloat = 0

        widthPerItem = collectionView.frame.width / 3 - 7
        heightPerItem = widthPerItem + 5

        return CGSize(width:widthPerItem, height: heightPerItem)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return namesArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = statsCollectionView.dequeueReusableCell(withReuseIdentifier: "GameStatsCollectionViewCell", for: indexPath) as! GameStatsCollectionViewCell
        cell.name.text = namesArray[indexPath.row]
        cell.points.text = numbers[indexPath.row]
        
        return cell
    }
}
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
        }
    }
star

Fri Jan 27 2023 17:08:22 GMT+0000 (Coordinated Universal Time)

#ios #swift #collectionview #collection #cv
star

Mon May 23 2022 05:09:17 GMT+0000 (Coordinated Universal Time)

#ios #swift #collectionview #collection #flow #flowlayout
star

Wed Jan 12 2022 05:32:21 GMT+0000 (Coordinated Universal Time)

##ios ##swift #collectionview #collection #view

Save snippets that work with our extensions

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