如何在Swift中改变uitableViewCell中标签的文字颜色?

问题描述 投票:0回答:1

我在tableViewCell中有一个标签是“lblProjeDurumu”。我从soap webServices获取数据到此标签。数据是“Devam Ediyor”或“Başlamadı”。我希望如果传入的数据是“Devam Ediyor”,则lblProjeDurumu的textColor为绿色,如果传入的数据为“Başlamadı”,则lblProjeDurumu的textColor为红色。我的代码在这里。

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

}

但没有变化。文本颜色lbl Proje Durumu仍然是默认颜色。如何根据传入的数据更改tableViewCell中的Label的textColor。

swift uitableview
1个回答
0
投票

只有在您设置的任何条件下进入时,才会更改颜色。您是否检查过任何这些条件是否真的在执行?

if ( cell.lblProjeDurumu.text == "Başlamadı"){
    cell.lblProjeDurumu.textColor = UIColor.red
}
else if ( cell.lblProjeDurumu.text == "Devam Ediyor"){
    cell.lblProjeDurumu.textColor = UIColor.green
}
© www.soinside.com 2019 - 2024. All rights reserved.