2017-11-30 32 views
-2

文字列が赤の場合、ラベルの色を変更したいと思います。私の最初の問題は、文字列からintを作ることです。私はこれをやろうとしましたが、うまくいかないようです。私は何が間違っているのか分からない。エラー:ifステートメントでラベルの色を変更してください

Cannot invoke initializer for type 'Int' with an argument list of type '([String])'

助けてください。

var percent_change_24hArray = [String]() 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell 

     let PercentInt = Int(percent_change_24hArray) 

     if PercentInt < 0 { 
      cell.procentLabel.textColor = UIColor.red 
     } 

     cell.procentLabel.text = "\(percent_change_24hArray[indexPath.row])%" 

     return cell 
    } 

答えて

2

は、あなたはおそらくないすべての文字列を整数に変換することができますので、今、あなたが受け取る整数はオプションです

let PercentInt = Int(percent_change_24hArray[indexPath.row]) 

を使用します。あなたは、開発者として、これが実際には起こり得ないことを知っているか、どちらの場合には、!を行末に付け加えるべきです。 の場合は、必要に応じてif let構造を使用して、オプションで安全にラップを解除する必要があります。

+0

もちろん、これは 'percent_change_24hArray'配列内の各要素に対して正確に1つの行があることを前提としています。 – rmaddy

+0

ありがとうございましたが、今は別の問題があります。 'cell.procentLabel.text =" \(percent_change_24hArray [indexPath.row])% "'はその値が例えば-17であるため、この値はnilですが、値はnilではないと言います。 – Danjhi

+0

'PerlInt = Int(percent_change_24hArray [indexPath.row])、PercentInt <0 {...}' – Annjawn

関連する問題