无法在自定义表格视图单元格中加载图像



我在自定义表视图单元格上设置的图像视图中加载图像时遇到问题。所有图像视图都使用相同的图像进行设置,但我的需要是根据我在代码中的要求设置图像。这是我为加载图像而实现的代码。

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
}

请注意,我已经检查了 assets 文件夹中的图像名称,它们都是正确的,我为国家/地区名称设置的条件也是正确的。所有国家/地区名称都显示在我的表格视图单元格中。

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")
}

使用此代码。

开关大小写让您更轻松

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;
}

请查看我发布的更新答案,如果您对此代码有任何问题,请告诉我。

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
}

相关内容

  • 没有找到相关文章

最新更新