2017-11-30 7 views
-2

문자열이 0 일 때 레이블의 색상을 변경하고 싶습니다. < 빨간색. 내 첫 번째 문제는 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' 배열의 각 요소에 정확히 하나의 행이 있다고 가정합니다. – rmaddy

+0

고마워,하지만 지금은 다른 문제가있다. 그것은 값이 nil이지만, 'cell.procentLabel.text = "\ (percent_change_24hArray [indexPath.row]) %"'가 -17과 같기 때문에 값이 nil이 아니라고 말합니다. – Danjhi

+0

if PerIntInt = Int (percent_change_24hArray [indexPath.row]), PercentInt <0 {...} ' – Annjawn