label is nil but it shows text on my app

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

Answered by OOPer in 272897022

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
        //...
    }

Where do you put this code ? (if(eventStatus.text == "Found"){ … )


You do not say where and how eventStatus.text is set.

it is in here (the cell for the collectionViewcontroller)

import UIKit
class eventsCollectionViewCell: UICollectionViewCell {
   
    var eventImageView: UIImageView!
    var eventTitle: UILabel!
    var eventDate: UILabel!
    var eventStatus: UILabel!
    var Eventvenue = String()
    var tabCat: UIImageView!
    var foundlostHeader = String()
   
    override func awakeFromNib() {
       
        tabCat = UIImageView(frame: CGRect.init(x: 75, y: 0, width: 50, height: 50))
        tabCat.clipsToBounds = true
        eventImageView = UIImageView(frame: contentView.frame)
        eventImageView.contentMode = .scaleAspectFill
        eventImageView.clipsToBounds = true
        eventTitle = UILabel()
        eventDate = UILabel()
        eventStatus = UILabel()
       
       
        /
        eventTitle.frame = CGRect(x: 0, y: frame.height-30, width: frame.width, height: 30)
        eventTitle.textColor = UIColor(r: 255, g: 255, b: 255)
        eventTitle.backgroundColor = UIColor.red.withAlphaComponent(0.3)
        eventTitle.textAlignment = .center
        eventTitle.font = eventTitle.font.withSize(12)
       
        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)
       
        eventDate.frame = CGRect(x: 0, y: frame.height-150, width: frame.width, height: 30)
        eventDate.textColor = UIColor(r: 255, g: 255, b: 255)
       
            if(self.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)
            }
       
        eventDate.textAlignment = .center
        eventDate.font = eventDate.font.withSize(8)
        /
        contentView.addSubview(eventImageView) /
        contentView.addSubview(eventStatus)
        contentView.addSubview(eventTitle)
        contentView.addSubview(eventDate)
        contentView.addSubview(tabCat)
    }
   
}

In awakeFromNib, you create eventStatus


     eventStatus = UILabel()

At this stage its text is empty (nil)


Then, you never set

     self.eventStatus.text

So during test, it is nil.

oh yeah.... so they are actually been set afterwards then in my UICollectionViewController:


override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let eventCell = cell as! eventsCollectionViewCell
        let event = events[indexPath.row]
        let theImageURL = event.rsEventImage
        eventCell.eventImageView.loadImageUsingCacheWithUrlString(theImageURL!)
        eventCell.eventTitle.text = event.rsEvent
        eventCell.eventDate.text = event.rsEventDate
        eventCell.eventStatus.text = event.rsEventStatus
        eventCell.Eventvenue = event.rsEventVenue
    }


Could you recommend a different way around this please?! hmm

What does your view hierarchy look like? Tableview anywhere?

Accepted Answer

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
        //...
    }

YAY! THANK YOU!!!

label is nil but it shows text on my app
 
 
Q