Mevlüt Soner
Mevlüt Soner

Reputation: 161

How to change text color of label in tableViewCell in Swift?

I have a label in tableViewCell that is "lblProjeDurumu" . I get data from soap webServices to this label. The data is "Devam Ediyor" or "Başlamadı". I want to that if the incoming data is "Devam Ediyor" , the textColor of lblProjeDurumu is green , if the incoming data is "Başlamadı", textColor of lblProjeDurumu is red. My code is here.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "projelerCell", for: indexPath) as! ProjelerCell

    cell.contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)

    // Fill the projeler cell

    cell.lblProjeAdi.text =  ( projeler.object(at: indexPath.row) as AnyObject ).value(forKey: "Proje_Adi") as? String

    cell.lblProjeDurumu.text =  ( projeler.object(at: indexPath.row) as AnyObject ).value(forKey: "Durumu") as? String

    cell.lblTarih.text =  ( projeler.object(at: indexPath.row) as AnyObject ).value(forKey: "Kayit_Tarihi") as? String

    if ( cell.lblProjeDurumu.text == "Başlamadı"){
        cell.lblProjeDurumu.textColor = UIColor.red
    }
    else if ( cell.lblProjeDurumu.text == "Devam Ediyor"){
        cell.lblProjeDurumu.textColor = UIColor.green
    }

    return cell

}

But there is no change. The textColor of lblProjeDurumu is still default color. How to change the textColor of Label in tableViewCell acording to incoming data.

Upvotes: 2

Views: 2266

Answers (1)

Oxthor
Oxthor

Reputation: 522

Your color is only changed if it enters in any of the conditions you have set. Have you checked that any of those conditions are really executing?

if ( cell.lblProjeDurumu.text == "Başlamadı"){
    cell.lblProjeDurumu.textColor = UIColor.red
}
else if ( cell.lblProjeDurumu.text == "Devam Ediyor"){
    cell.lblProjeDurumu.textColor = UIColor.green
}

Upvotes: 1

Related Questions