Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法使用if条件Swift 4.0更改文本的颜色_Ios_Swift_Tableview - Fatal编程技术网

Ios 无法使用if条件Swift 4.0更改文本的颜色

Ios 无法使用if条件Swift 4.0更改文本的颜色,ios,swift,tableview,Ios,Swift,Tableview,下面是我的代码。我试图通过使用if条件来更改tableViewCell中文本的颜色,如果字符串包含-符号,则它应该变为红色或绿色,但结果显示所有字符串都变为绿色 cell.percentLabel.text = PercentChange[indexPath.row] for i in PercentChange{ if (i.range(of: "-") != nil) { cell.percentLabel.textColor = UIColor.red }

下面是我的代码。我试图通过使用
if
条件来更改tableViewCell中文本的颜色,如果字符串包含
-
符号,则它应该变为红色或绿色,但结果显示所有字符串都变为绿色

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

假设您发布的代码位于
cellForRowAt
方法中,您不应该循环使用所有
PercentChange

如前所述,您可以根据
PercentChange
中的最后一个值设置每个单元格的颜色

您应该只查看特定于给定行的值

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

您需要重新加载表视图以更新单元格内的内容。此代码采用什么方法?提供一些上下文。您需要重新加载表视图以更新单元格内的内容。此代码使用什么方法?提供一些上下文。