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()
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With