如何以编程方式将图像添加到现有tableView的单元格中? [英] How to add images [programmatically] to cells of existing tableView?
问题描述
我需要将图像添加到与tableView单元格相同名称的tableView单元格中.即波音"单元格使用"Boeing.png"图片.有些图像为.jpg格式.
I need to add images to tableView cells of the same names as the tableView cells. i.e. 'Boeing' cell uses 'Boeing.png' image. Some images are of .jpg format.
我尝试以与节和行布局相同的方式添加图像数组.图片与单元格具有相同的名称.我在尝试进行更改的地方添加了注释(#1-4).这应该很简单,但是我看不到我所缺少的.
I have tried adding an array of images in the same manner as the section and row layout. The pictures have the same names as the cells. I have added comments(#1-4) to where I have tried to make changes. This should be rather simple, but I cannot see what I am missing.
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))
}
}
我在这段代码中遇到的错误是:
The errors I am getting with this code are:
源文件中的编辑器占位符",'((键:字符串,值:[字符串])'不能转换为'(_,_,_)',元组的数目不同...", 定义与先前的冲突..." @ 线: 、、、 用于运输方式中的(键,值){ 、、
"Editor placeholder in source file", "'(key: String, value: [String])' is not convertible to '(_, _, _)', tuples have a different number of...", "Definition conflicts with previous ..." @ line: ,,, 'for (key, value) in transportMeans {' ,,,
推荐答案
您的transportMeans
具有类型:[String : [String]]
.因此,在for循环中,您应该执行以下操作:
Your transportMeans
has type: [String : [String]]
. So in the for loop you should do something like this:
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))
}
这篇关于如何以编程方式将图像添加到现有tableView的单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!