我在自定义表格视图单元格中设置的图像视图中加载图像时遇到问题。所有图像视图都使用相同的图像进行设置,但我需要根据代码中的要求设置图像。这是我为加载图像而实现的代码。
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
}
请注意我已检查资源文件夹中图片的名称,它们都是正确的,我为国家/地区名称添加的条件也是正确的。所有国家/地区名称都显示在我的表视图单元格。
答案 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")
}
使用此代码。
答案 1 :(得分: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;
}
答案 2 :(得分: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")
}
在此处设置一个断点并调试以查找CountryArray [indexPath.row]的值并正确检查图像名称
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
}