简体   繁体   English

如何在Swift中的tableViewCell中更改标签的文本颜色?

[英]How to change text color of label in tableViewCell in Swift?

I have a label in tableViewCell that is "lblProjeDurumu" . 我在tableViewCell中有一个标签为“ lblProjeDurumu”。 I get data from soap webServices to this label. 我从soap webServices到此标签获取数据。 The data is "Devam Ediyor" or "Başlamadı". 数据为“ Devam Ediyor”或“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. 我想如果传入数据是“ Devam Ediyor”,则lblProjeDurumu的textColor为绿色,如果传入数据是“Başlamadı”,则lblProjeDurumu的textColor为红色。 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. lblProjeDurumu的textColor仍然是默认颜色。 How to change the textColor of Label in tableViewCell acording to incoming data. 如何根据传入数据更改tableViewCell中Label的textColor。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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