Snippets Collections
import UIKit

class CustomTableViewCell: UITableViewCell {
    var labelStackView: UIStackView!
    var innerView: UIView!
    var label: UILabel!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupInnerView()
        setupLabelStackView()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupInnerView()
        setupLabelStackView()
    }
    
    private func setupInnerView() {
        
            innerView = UIView()
            innerView.translatesAutoresizingMaskIntoConstraints = false
            innerView.layer.borderWidth = 1.0 // 1-point border width
            innerView.layer.borderColor = UIColor.lightGray.cgColor // Border color
            
            contentView.addSubview(innerView)

            NSLayoutConstraint.activate([
                innerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10), // 8-point spacing from the left
                innerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 6), // 8-point spacing from the top
                innerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10), // 8-point spacing from the right
                innerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6) // 8-point spacing from the bottom
            ])
        
        innerView.layer.cornerRadius = 13
        innerView.layer.borderColor = self.hexStringToUIColor(hex: "DCD8D8").cgColor
    }
    
    private func setupLabelStackView() {
        labelStackView = UIStackView()
        labelStackView.translatesAutoresizingMaskIntoConstraints = false
        labelStackView.axis = .vertical
        labelStackView.spacing = 8 // Adjust the vertical spacing between labels
        contentView.addSubview(labelStackView)

        NSLayoutConstraint.activate([
            labelStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
            labelStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
            labelStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
            labelStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16)
        ])
    }
    
    func setupLabels(labelData: [(String, UIImage, Int)]) -> ([UILabel], [UIImageView], [Int]) {
        
        var labels: [UILabel] = []
        var imageViews: [UIImageView] = []
        var tags: [Int] = []

        for (labelText, image, tag) in labelData {
            print("tagfkj : ", tag)
            let stackView = UIStackView()
            stackView.axis = .horizontal
            stackView.spacing = 8

            let imageView = UIImageView(image: image)
            imageView.contentMode = .scaleAspectFit
            imageView.widthAnchor.constraint(equalToConstant: 23).isActive = true
            imageView.heightAnchor.constraint(equalToConstant: 23).isActive = true
            imageView.tag = tag
            imageView.accessibilityIdentifier = "sewer"
            
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.text = labelText
            label.tag = tag
            
            stackView.addArrangedSubview(imageView)
            stackView.addArrangedSubview(label)

            labelStackView.addArrangedSubview(stackView)

            labels.append(label)
            imageViews.append(imageView)
            tags.append(tag)
        }

        return (labels, imageViews, tags)
    }
    
    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }

        if ((cString.count) != 6) {
            return UIColor.gray
        }

        var rgbValue:UInt64 = 0
        Scanner(string: cString).scanHexInt64(&rgbValue)

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}






func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  var imagesArray = ["circle_complete_icon", "circle_unsel"]()
            var textArray = ["jfdsk", "dfs"]()
            var tag = indexpath.row
            var idsArray = ["1","2"]()

      tag = indexPath.row
            print("tag43 : \(indexPath.row) ", tag)
            
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
            cell.selectionStyle = .none
            
            for subview in cell.labelStackView.arrangedSubviews {
                cell.labelStackView.removeArrangedSubview(subview)
                subview.removeFromSuperview()
            }
            
            var count = 0
            let labelsAndImagesForCell = textArray.enumerated().map { (index, labelText) -> (String, UIImage, Int) in
                let imageName = imagesArray[count]
                count += 1
                let image = UIImage(named: imageName) ?? UIImage()
                
                // Replace "UIImage()" with your default image if needed
                print("chk378: ", indexPath.row)
                return (labelText, image, tag)
            }
            
            let (labels, imageViews, tags) = cell.setupLabels(labelData: labelsAndImagesForCell)
            
            // Access and configure each label and image view individually
            var c = 0
            for (index, label) in (labels).enumerated() {
                label.text = labelsAndImagesForCell[index].0
                let labelTapGesture = UITapGestureRecognizer(target: self, action: #selector(subCategoryPressed(tagGesture: )))
                label.tag = tags[c]
                label.accessibilityIdentifier = idsArray[c]
                c += 1
                label.isUserInteractionEnabled = true
                label.addGestureRecognizer(labelTapGesture)
            }
            
            var c1 = 0
            for (index, imageView) in imageViews.enumerated() {
                imageView.image = labelsAndImagesForCell[index].1
                
                let tapGesture = UITapGestureRecognizer(target: self, action: #selector(subCategoryPressed(tagGesture: )))
                imageView.tag = tags[c1]
                imageView.accessibilityIdentifier = idsArray[c1]
                c1 += 1
                imageView.isUserInteractionEnabled = true
                imageView.addGestureRecognizer(tapGesture)
            }
            
            imagesArray = []
            textArray = []
            
            return cell
            
        }

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

Wed Aug 02 2023 07:18:27 GMT+0000 (Coordinated Universal Time)

#ios #swift #cell #custom #customcell
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