mack mack - 1 year ago 80
iOS Question

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()


Answer Source

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