How to change the color of the label in the collection view cell by clicking

I have one collection view with some cells. Inside my cells, I have one shortcut to show a list of country names. And each time I click on each label, I will show some data in the form of a table under my collection view.

When I click on any cell label, I need to turn this label text in red. And other label names should be black.

How to do it? I tried the code below, but when I select any label name in this cell, all the cell labels turn red.

How to fix it?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


        if let cell = collectionView1.cellForItemAtIndexPath(indexPath) {

            var label = cell.viewWithTag(100) as? UILabel


            label!.textColor = UIColor.redColor()

        }
    }
+4
source share
2 answers

, . , , - setSelected

:


override var setSelected(selected: bool) {
    if (selected) { 
       self.youLabel!.textColor = UIColor.redColor()
    } else {
       self.youLabel!.textColor = UIColor.blackColor()
}

. , - iPhone

+2

, . , custom collectionview cell.

-1

Source: https://habr.com/ru/post/1648536/


All Articles