Snippets Collections
/** select-text.js
 * Set selected text to the element passed to the function.
 */
function selectText(el) {
	let sel, range;
	// Browser compatibility
	// older ie
	if (window.getSelection && document.createRange) {
		sel = window.getSelection();
		// creates range object
		// set range to elements range
		// remove window range
		// set window range
		window.setTimeout(function() {
			range = document.createRange();
			range.selectNodeContents(el);
			sel.removeAllRanges();
			sel.addRange(range);
		}, 1);
	} else if (document.selection) {
		sel = document.selection.createRange();
		// creates range object
		// set raselectnge
		// call  event listener
		if (sel.text == '') {
			range = document.body.createTextRange();
			range.moveToElementText(el);
			range.select();
		}
	} else {
		// do nothing
	}
	return;
}
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()
    }
    
}
     let currentSelectedCell = playersTableView.cellForRow(at: indexPath) as! PlayerDetailsTableViewCell
        
        if selectedRow == -1 {
            currentSelectedCell.playerDetailsView.layer.borderColor = hexStringToUIColor(hex: "F34141").cgColor
            currentSelectedCell.playerDetailsView.layer.borderWidth = 1
            selectedRow = indexPath.row
        } else if indexPath.row == selectedRow {
            currentSelectedCell.playerDetailsView.layer.borderWidth = 0
            selectedRow = -1
        } else {
            currentSelectedCell.playerDetailsView.layer.borderColor = hexStringToUIColor(hex: "F34141").cgColor
            currentSelectedCell.playerDetailsView.layer.borderWidth = 1
            
            let newIndexPath = IndexPath(item: selectedRow, section: 0)
            if let cell = playersTableView.cellForRow(at: newIndexPath) as? PlayerDetailsTableViewCell {
                cell.playerDetailsView.layer.borderWidth = 0
            }
            selectedRow = indexPath.row
        }
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")
                }
        }
    }
}
star

Thu Jul 20 2023 03:14:07 GMT+0000 (Coordinated Universal Time)

#select #text #selection
star

Wed Mar 01 2023 08:01:14 GMT+0000 (Coordinated Universal Time)

#ios #swift #cell #select #selection #single

Save snippets that work with our extensions

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