簡體   English   中英

如果條件為Swift 4.0,則無法更改文本的顏色

[英]Cannot change color of the text using if condition Swift 4.0

下面是我的代碼。 我正在嘗試通過使用if條件來更改tableViewCell內文本的顏色, if條件包含字符串-符號,則它應該變為紅色,否則變為綠色,但事實是所有字符串都變為綠色。

cell.percentLabel.text = PercentChange[indexPath.row]

for i in PercentChange{
    if (i.range(of: "-") != nil) {
        cell.percentLabel.textColor = UIColor.red
   } else {
        cell.percentLabel.textColor = UIColor.green
    }
}

假設您發布的代碼在cellForRowAt方法中,則不應遍歷所有PercentChange值。

如所寫,您將根據PercentChange的最后一個值設置每個單元格的顏色。

您應該只查看特定於給定行的值。

let change = PercentChange[indexPath.row]
cell.percentLabel.text = change
cell.percentLabel.textColor = change.range(of: "-") != nil ? .red : .green

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM