我需要将图像添加到与 tableView 单元格同名的 tableView 单元格中。即“波音”单元格使用“Boeing.png”图像。一些图像是 .jpg 格式。
我尝试以与部分和行布局相同的方式添加一组图像。图片与单元格具有相同的名称。我在我试图进行更改的地方添加了评论(#1-4)。这应该相当简单,但我看不到我错过了什么。
import UIKit
class NewTableViewController: UITableViewController {
let transportMeans = ["Cars" : ["Ferrari", "Chrysler", "Toyota"], "Trains" : ["Steam", "Diesel", "Electric"], "Planes" : ["AirBus", "Boeing", "Cessna"]]
// #1 added a line for images to be applied:
var transportImages = ["Cars" : ["Ferrari.png", "Chrysler.png","Toyota.png"], "Trains" : ["Steam.png","Diesel.png","Electric.png"], "Planes" : ["AirBus.jpg","Boeing.jpg","Cessna.png"]]
var selectedFileName = ""
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
//#2 added a line for images:
var transportImages : String!
}
var objectArray = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
// #3 added term 'value' to key:
for (key, value, value) in transportMeans {
print("\(key) -> \(value) -> \(value)")
//#4 added a line for 'transportImages' in the append section-------:
objectArray.append(Objects(sectionName: key, sectionObjects: value, transportImages: value))
}
}
我使用此代码遇到的错误是:
"源文件中的编辑器占位符", "'(key: String, value: [String])' 不能转换为 '(_, _, _)', 元组有不同数量的...", "定义冲突与以前的 ..." @ line: ,,, 'for (key, value) in transportMeans {' ,,,
您的transportMeans
类型:[String : [String]]
。所以在 for 循环中你应该做这样的事情:
for (key, value) in transportMeans {
print("\(key) -> \(value) -> \(value)")
//#4 added a line for 'transportImages' in the append section-------:
objectArray.append(Objects(sectionName: key, sectionObjects: value, transportImages: key))
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句