Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the label colour in collection view cell by click

I have one collection view with some cells. Inside my cells I have one label to show a list of country names. And on every click of each cell labels I will display some data in table view below my collection view.

When I press any cell label I need to turn that label's text red. And other label names should be black.

How to do that? I have tried the code below, but when I select any label name in that cell all the cell labels are turning red.

How do I fix this?

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


        if let cell = collectionView1.cellForItemAtIndexPath(indexPath) {

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


            label!.textColor = UIColor.redColor()

        }
    }
like image 985
mack Avatar asked Nov 08 '22 12:11

mack


1 Answers

Your label doesn't update because you need to refresh cells. So best solution I could offer you - change your label color in setSelected method in your custom cell's class

Edit:


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

This is example of function in your custom class. Sorry, if there are mistakes - answered from iPhone

like image 78
Igor Kislyuk Avatar answered Nov 14 '22 21:11

Igor Kislyuk