简体   繁体   中英

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM