ok so I have a label called eventStatus
var eventStatus: UILabel!
eventStatus = UILabel()
eventStatus.frame = CGRect(x: 0, y: frame.height-60, width: frame.width, height: 30)
eventStatus.textColor = UIColor(r: 255, g: 255, b: 255)
eventStatus.backgroundColor = UIColor.red.withAlphaComponent(0.3)
eventStatus.textAlignment = .center
eventStatus.font = eventStatus.font.withSize(14)
contentView.addSubview(eventStatus)
great this all works fine and my label shows different status in my collectionViewController
However, I would like to style my cells depending on what the status text reads but when I do the following statement, nothing happens
if(eventStatus.text == "Found"){
tabCat.image = UIImage(named: "found-tab")
eventTitle.backgroundColor = UIColor.green.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.green.withAlphaComponent(0.3)
} else if(eventStatus.text == "Lost"){
tabCat.image = UIImage(named: "lost-tab")
eventTitle.backgroundColor = UIColor.red.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.red.withAlphaComponent(0.3)
} else if(eventStatus.text == "brokenglass"){
tabCat.image = UIImage(named: "hazard-tab")
eventTitle.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
} else if(eventStatus.text == "flooding"){
tabCat.image = UIImage(named: "hazard-tab")
eventTitle.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
} else if(eventStatus.text == "graffiti"){
tabCat.image = UIImage(named: "hazard-tab")
eventTitle.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
}
eventStatus.text is showing as nil when I debug
Please, any help here is much appreciated
Thanks
One way, create a computed property.
class EventsCollectionViewCell: UICollectionViewCell {
//...
private var eventStatus: UILabel!
//...
var eventStatusText: String? {
get {
return eventStatus.text
}
set {
eventStatus.text = newValue
switch(newValue) {
case "Found"?:
tabCat.image = UIImage(named: "found-tab")
eventTitle.backgroundColor = UIColor.green.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.green.withAlphaComponent(0.3)
case "Lost"?:
tabCat.image = UIImage(named: "lost-tab")
eventTitle.backgroundColor = UIColor.red.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.red.withAlphaComponent(0.3)
case "brokenglass"?:
tabCat.image = UIImage(named: "hazard-tab")
eventTitle.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
case "flooding"?:
tabCat.image = UIImage(named: "hazard-tab")
eventTitle.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
case "graffiti"?:
tabCat.image = UIImage(named: "hazard-tab")
eventTitle.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
eventDate.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
default:
break
}
}
}
override func awakeFromNib() {
//...
//remove all `if(self.eventStatus.text == ...) ...` lines
//...
}
}
And use it as:
override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let eventCell = cell as! EventsCollectionViewCell
//...
eventCell.eventStatusText = event.rsEventStatus
//...
}