无法加载自定义TableView中的图像Cell
我正在加载在自定义表格视图单元格中设置的图像视图中的图像问题。所有的图像视图都设置了相同的图像,但我的需要是设置图像根据我在代码中的要求。这是我为加载图像所实现的代码。无法加载自定义TableView中的图像Cell
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国名出现在我的表格视图单元格。
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")
}
使用此代码。
谢谢。它为我工作:) –
当你只使用if语句,然后检查所有条件,这就是为什么未知的图像显示和标记接受@SamarthKejriwal – salmancs43
查看我发布的更新回答,如果您对此代码有任何问题,请告诉我们。
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
}
开关箱使其更容易你
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;
}
其相同的我猜测 –
检查在非洲的图像出口CellCell类 – iUser
不是一个问题,因为我的ima ge正在被加载,但它并没有遵循if条件,它直接进入else条件并加载“未知”图像 –
只有最后一个单元格根据所需图像加载 –