2017-06-19 20 views
0

カスタムテーブルビューのセルに設定されている画像ビューで画像を読み込む際に問題があります。すべての画像ビューが同じ画像で設定されていますが、コード内の私の要件に応じて画像。これは私がイメージをロードするために実装したコードです。カスタムTableViewセルに画像を読み込むことができません

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



var curpricefloat : Double = Double(CurrPrice[indexPath.row])! 
curpricefloat = Double(round(100*curpricefloat)/100) 

var Chgfloat : Double = Double(Chg[indexPath.row])! 
Chgfloat = Double(round(100*Chgfloat)/100) 

var perchgfloat : Double = Double(PerChg[indexPath.row])! 
perchgfloat = Double(round(100*perchgfloat)/100) 


cell.Indicename?.text = Indicename[indexPath.row] 
cell.Country?.text = CountryArray[indexPath.row] 
cell.PerChg?.text = "(" + String(perchgfloat) + "%)" 
cell.CurrPrice?.text = String(curpricefloat) 
cell.Chg?.text = String(Chgfloat) 



if Float((cell.Chg?.text)!)! < 0 { 

    Chgfloat = abs(Chgfloat) 
    perchgfloat = abs(perchgfloat) 


    cell.PerChg?.text = "(" + String(perchgfloat) + "%)" 
    cell.Chg?.text = String(Chgfloat) 

    cell.Chg?.textColor = UIColor.red 
    cell.PerChg?.textColor = UIColor.red 
    cell.arrow?.image = UIImage(named : "arrowdown") 

} else 
{ 
    cell.Chg?.textColor = UIColor.green 
    cell.PerChg?.textColor = UIColor.green 
    cell.arrow?.image = UIImage(named : "arrowup") 
} 

    /* This is the code which i have written for loading the images , 
but all the cells are being loaded with the image named : "unknown" 
except the last cell which are having the name of country as "South Africa" */ 


if cell.Country!.text! == "Egypt"{ 
    cell.countryFlag?.image = UIImage(named : "egypt") 
} 
if cell.Country!.text! == "Kenya"{ 
    cell.countryFlag?.image = UIImage(named : "kenya") 
} 
if cell.Country!.text! == "Namibia"{ 
    cell.countryFlag?.image = UIImage(named : "namibia") 
} 
if cell.Country!.text! == "South Africa"{ 
    cell.countryFlag?.image = UIImage(named : "south_africa") 
} 
else{ 
    cell.countryFlag?.image = UIImage(named : "unknown") 
} 

return cell 
} 

私は彼らのすべてが正しい、資産フォルダ内の画像の名前をチェックしていると私は国の名のために入れている条件は名前が私の中に表示される国をもcorrect.Allさに注意してください。テーブルビューセル。

+0

アフリカの画像のコンセントを確認してください。セルクラス – iUser

+0

私のイマgeがロードされているがif条件に従っていない場合は、直接else条件に行き、 "unknown"イメージをロードしています –

+0

最後のセルのみが目的のイメージに従ってロードされています –

答えて

2
if cell.Country!.text! == "Egypt"{ 
    cell.countryFlag?.image = UIImage(named : "egypt") 
} 
else if cell.Country!.text! == "Kenya"{ 
    cell.countryFlag?.image = UIImage(named : "kenya") 
} 
else if cell.Country!.text! == "Namibia"{ 
    cell.countryFlag?.image = UIImage(named : "namibia") 
} 
else if cell.Country!.text! == "South Africa"{ 
    cell.countryFlag?.image = UIImage(named : "south_africa") 
} 
else{ 
    cell.countryFlag?.image = UIImage(named : "unknown") 
} 

このコードを使用してください。

+0

ありがとうございました:) –

+0

if文だけを使用すると、すべての条件がチェックされ、未知の画像が表示され、@SamarthKejriwalというマークが付けられている理由を確認しました – salmancs43

1

更新された回答をチェックしてください。このコードに問題がある場合はお知らせください。

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



var curpricefloat : Double = Double(CurrPrice[indexPath.row])! 
curpricefloat = Double(round(100*curpricefloat)/100) 

var Chgfloat : Double = Double(Chg[indexPath.row])! 
Chgfloat = Double(round(100*Chgfloat)/100) 

var perchgfloat : Double = Double(PerChg[indexPath.row])! 
perchgfloat = Double(round(100*perchgfloat)/100) 


cell.Indicename?.text = Indicename[indexPath.row] 
cell.Country?.text = CountryArray[indexPath.row] 
cell.PerChg?.text = "(" + String(perchgfloat) + "%)" 
cell.CurrPrice?.text = String(curpricefloat) 
cell.Chg?.text = String(Chgfloat) 



if Float((cell.Chg?.text)!)! < 0 { 

Chgfloat = abs(Chgfloat) 
perchgfloat = abs(perchgfloat) 


cell.PerChg?.text = "(" + String(perchgfloat) + "%)" 
cell.Chg?.text = String(Chgfloat) 

cell.Chg?.textColor = UIColor.red 
cell.PerChg?.textColor = UIColor.red 
cell.arrow?.image = UIImage(named : "arrowdown") 

} else { 
    cell.Chg?.textColor = UIColor.green 
    cell.PerChg?.textColor = UIColor.green 
    cell.arrow?.image = UIImage(named : "arrowup") 
} 

[indexPath.row] CountryArrayの値を見つけるためにこことデバッグブレークポイントを入れても、イメージ名を適切

if CountryArray[indexPath.row] == "Egypt"{ 
    cell.countryFlag?.image = UIImage(named : "egypt") 
} else if CountryArray[indexPath.row] == "Kenya"{ 
    cell.countryFlag?.image = UIImage(named : "kenya") 
} else if CountryArray[indexPath.row] == "Namibia"{ 
    cell.countryFlag?.image = UIImage(named : "namibia") 
} else if CountryArray[indexPath.row] == "South Africa"{ 
    cell.countryFlag?.image = UIImage(named : "south_africa") 
} else{ 
    cell.countryFlag?.image = UIImage(named : "unknown") 
} 

return cell 
} 
+0

私は上記の解決策から回答を得ました –

+0

素晴らしい!!乾杯~~ !! – iUser

+0

ありがとう男.... :) –

2

スイッチケースがために、それがより簡単に確認してくださいあなた

switch(cell.Country!.text!){ 
case "Egypt" : 
    cell.countryFlag?.image = UIImage(named : "egypt") 
    break 
case "Kenya" : 
    cell.countryFlag?.image = UIImage(named : "kenya") 
    break 
case "Namibia" : 
    cell.countryFlag?.image = UIImage(named : "namibia") 
    break 
case "South Africa" : 
    cell.countryFlag?.image = UIImage(named : "south_africa") 
    break 
default : 
    cell.countryFlag?.image = UIImage(named : "unknown") 
    break; 
} 
+0

その同じ私が推測する –

関連する問題