我正在努力用 php 服务填充来自 json 脚本的内容填充 iOS 表视图 (swift(。我已正确填充内容,但我正在尝试根据表内容的特征显示本地图像图标。
我尝试了两种选择: 1.只需直接分配图像;
myCell.headlineLabel!.text = item.title
myCell.sourceLabel!.text = item.source
myCell.sourceIcon!.image = UIImage(named: "bbc.png")
根据传入项的值有条件地分配内容。
if(item.source == "BBC Business"({ myCell.sourceIcon!.Image = UIImage(命名:"bbc.png"(}else if(item.source == "BBC Politics"({ myCell.sourceIcon!.Image = UIImage(命名:"bbc.png"(}else if(item.source == "CNN Money"({ myCell.sourceIcon!.image = UIImage(命名:"CNN Icon.png"(}else if(item.source == "CNN Markets"({ myCell.sourceIcon!.image = UIImage(命名:"CNN Icon.png"(}
我想知道是否有人对此有任何经验,或者是否有可能。
我真的很感激任何反馈!
谢谢亚历克斯·:)
最简单的方法
switch item.source {
case "BBC Business","BBC Politics":
myCell.sourceIcon!.image = UIImage(named: "bbc")
default:
myCell.sourceIcon!.image = UIImage(named: "CNNIcon")
//watch out with the space on UIImage name
}
做到这一点的优雅方法是使用 ENUM。