我想根据获取的json数据在tableview中显示特定图像。我的图像在本地文件夹中的资产中。
假设json是:
[
{
"name": "john",
"car": "bmw",
},
{
"name": "mike",
"car": "audi",
}
{
"name": "ana",
"car": null,
}
{
"name": "nick",
"car": "mazda",
}
]
我的资产图片:
bmw.jpg
audi.jpg
nothing.jpg // to display "ana" image
我没有mazda.jpg图片。
财产:
var name: [String] = []
var image: [String] = []
所以我将json数据解析为字符串数组...而表单元格有问题
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
let imageName = image[indexPath.row]
cell.imageView.image = UIImage(named: imageName)
我的目标是,如果“名称”为“ john”,则在表视图中显示本地图像“ bmw”
编辑:
我设法显示了图像,但如何处理null和“ mazda”
我尝试像这样的switch语句:
switch imageName {
case "" :
cell.imageLabel.image = UIImage(named: "nothing")
case imageName:
cell.imageLabel.image = UIImage(named: imageName)
case: // need code for all others that I dont have image
cell.imageLabel.image = UIImage(named: "nothing")
}
试试这个
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
let imageName = image[indexPath.row]
cell.imageView.image = UIImage(named: imageName)
return cell
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句