Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

really don't know how to set function in order to recognize whether cell is selected or not

Tags:

swift

class ViewController: UICollectionViewController {

var selectedRow : Int?

let data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier : "MyCell")
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return data.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CollectionViewCell
    cell.Label.text = String(data[indexPath.row])
    return cell
 }}



class CollectionViewCell: UICollectionViewCell {
  @IBOutlet weak var Label: UILabel!


@IBOutlet weak var button: UIButton!
@IBAction func buttonPressed(_ sender: UIButton) {
  if Label.textColor == .red{
   Label.textColor = .black
  } else if Label.textColor == .black{
   Label.textColor = .red
  }}


public override func awakeFromNib() {
  super.awakeFromNib()}}

I want to recognize whether cell is selected or not. but I really don't know where in I have to use what function. many people are saying using setSelected function but I think there is no such function. I'm beginner so I don't know well. what I want to is make "if I select one of that number then that cell's textColor turn red. and then I select another cell. then that cell's textColor turn red and original one turn black again."

what function I have to use and where I have to use function.

like image 768
신효근 Avatar asked Jan 20 '26 15:01

신효근


2 Answers

There are so many ways to do it, I prefer the below way.

Change: 1

You need to add buttonPressed method into UIViewController

Change: 2

You need to add code for UILabel text color into cellForItemAt.

Full code:

class ViewController: UICollectionViewController {
    
    var selectedRow : Int?
    
    let data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier : "MyCell")
    }
    
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CollectionViewCell
        
        cell.button.tag = indexPath.item
        cell.button.addTarget(self, action:#selector(buttonPressed(_:)) , for: .touchUpInside)
        cell.Label.text = String(data[indexPath.row])
        
        if selectedRow == indexPath.item{
            cell.Label.textColor = .black
        }else{
            cell.Label.textColor = .red
        }
        return cell
    }
    
    @objc func buttonPressed(_ sender: UIButton) {
        if selectedRow == sender.tag{
            selectedRow = nil
        }else{
            selectedRow = sender.tag
        }
        collectionView.reloadData()
    }
}
like image 67
Hitesh Surani Avatar answered Jan 22 '26 08:01

Hitesh Surani


You should overload UICollectionViewDelegate's collectionView(_:didSelectItemAt:) method in your ViewController class. It gets called whenever user taps on a cell.

like image 34
Gleb Shevchenko Avatar answered Jan 22 '26 10:01

Gleb Shevchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!