簡體   English   中英

如何在Swift中的tableViewCell中更改標簽的文本顏色?

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

我在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

}

但是沒有變化。 lblProjeDurumu的textColor仍然是默認顏色。 如何根據傳入數據更改tableViewCell中Label的textColor。

只有在您輸入的任何條件下,您的顏色才會更改。 您是否檢查過其中任何條件是否確實在執行?

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