Ios Swift 4:将数据从Tableview传递到ViewController

Ios Swift 4:将数据从Tableview传递到ViewController,ios,arrays,swift,uitableview,tableview,Ios,Arrays,Swift,Uitableview,Tableview,所以我在这上面花了两天的时间,我可以肯定地说我被卡住了 因此,我创建了一个链接到自定义单元格的tableview,我已经设置了一个项目数组,并且在构建它时每个部分都按正确的顺序排列,这一切都很好,但我似乎无法传递信息!哈哈。我总是出错!我将尽可能地详细说明我是新来的(并且是一名3周大的程序员#初学者),不确定我可以附加多少信息,但这里什么都没有: 以下是我的数组和表函数: var foodKind = [["Whole eggs", "Bacon", "16oz water"],["80% Gr

所以我在这上面花了两天的时间,我可以肯定地说我被卡住了

因此,我创建了一个链接到自定义单元格的tableview,我已经设置了一个项目数组,并且在构建它时每个部分都按正确的顺序排列,这一切都很好,但我似乎无法传递信息!哈哈。我总是出错!我将尽可能地详细说明我是新来的(并且是一名3周大的程序员#初学者),不确定我可以附加多少信息,但这里什么都没有:

以下是我的数组和表函数:

var foodKind = [["Whole eggs", "Bacon", "16oz water"],["80% Ground beef", "Avacado", "16oz water"],["Lettuce burger", "Avacado Salad", "16oz water"],["Tri-pep"],["MTS Machine Whey", "Tri-Pep"]]
var itemCount =  [["4 boiled", "3 Slice", "1 bottle"],["6oz", "1 whole", "1 bottle"],["1 burger", "2 cups", "1 bottle"], ["1 serving"], ["1 scoop", "1 serving"]]

var count = [3, 3, 3, 1, 2]

var foodImage = [[#imageLiteral(resourceName: "hard boiled eggs"), #imageLiteral(resourceName: "bacon"),#imageLiteral(resourceName: "water")], [#imageLiteral(resourceName: "ground beef"), #imageLiteral(resourceName: "avacdo"), #imageLiteral(resourceName: "water")],[ #imageLiteral(resourceName: "lettuce wrap burger"), #imageLiteral(resourceName: "avacado salad 1"), #imageLiteral(resourceName: "water")], [#imageLiteral(resourceName: "tri-pep aminos")], [#imageLiteral(resourceName: "mtswhey-2"), #imageLiteral(resourceName: "tri-pep aminos")]]

var sections = ["Breakfast: 450 calories", "Lunch: 650 calories", "Dinner: 543 calories","Pre-Workout calories: 0", "PostWorkout: 153 calories"]
var selectedIndex = Int()

    override func viewDidLoad() {
    super.viewDidLoad()

    dietTableView.delegate = self
   //dietTableView.dataSource = self

        self.dietTableView.register(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "custom")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section]

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return count[section]


}
func numberOfSections(in tableView: UITableView) -> Int {
   return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell : CustomTableViewCell = self.dietTableView.dequeueReusableCell(withIdentifier: "custom") as! CustomTableViewCell


    cell.itemName.text = foodKind[indexPath.section][indexPath.row]
    cell.itemCount.text = itemCount[indexPath.section][indexPath.row]
    cell.itemPicture.image = foodImage[indexPath.section][indexPath.row]
return cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectedIndex = indexPath.row
    performSegue(withIdentifier: "Item", sender: self)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Item" {
        let vc : ItemViewController = segue.destination as! ItemViewController



        vc.count = itemCount[selectedIndex]
        vc.image = foodImage[selectedIndex]
        vc.name = foodKind[selectedIndex]
以下是我的“didselectrow”和“preparesegue”功能:

var foodKind = [["Whole eggs", "Bacon", "16oz water"],["80% Ground beef", "Avacado", "16oz water"],["Lettuce burger", "Avacado Salad", "16oz water"],["Tri-pep"],["MTS Machine Whey", "Tri-Pep"]]
var itemCount =  [["4 boiled", "3 Slice", "1 bottle"],["6oz", "1 whole", "1 bottle"],["1 burger", "2 cups", "1 bottle"], ["1 serving"], ["1 scoop", "1 serving"]]

var count = [3, 3, 3, 1, 2]

var foodImage = [[#imageLiteral(resourceName: "hard boiled eggs"), #imageLiteral(resourceName: "bacon"),#imageLiteral(resourceName: "water")], [#imageLiteral(resourceName: "ground beef"), #imageLiteral(resourceName: "avacdo"), #imageLiteral(resourceName: "water")],[ #imageLiteral(resourceName: "lettuce wrap burger"), #imageLiteral(resourceName: "avacado salad 1"), #imageLiteral(resourceName: "water")], [#imageLiteral(resourceName: "tri-pep aminos")], [#imageLiteral(resourceName: "mtswhey-2"), #imageLiteral(resourceName: "tri-pep aminos")]]

var sections = ["Breakfast: 450 calories", "Lunch: 650 calories", "Dinner: 543 calories","Pre-Workout calories: 0", "PostWorkout: 153 calories"]
var selectedIndex = Int()

    override func viewDidLoad() {
    super.viewDidLoad()

    dietTableView.delegate = self
   //dietTableView.dataSource = self

        self.dietTableView.register(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "custom")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section]

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return count[section]


}
func numberOfSections(in tableView: UITableView) -> Int {
   return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell : CustomTableViewCell = self.dietTableView.dequeueReusableCell(withIdentifier: "custom") as! CustomTableViewCell


    cell.itemName.text = foodKind[indexPath.section][indexPath.row]
    cell.itemCount.text = itemCount[indexPath.section][indexPath.row]
    cell.itemPicture.image = foodImage[indexPath.section][indexPath.row]
return cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectedIndex = indexPath.row
    performSegue(withIdentifier: "Item", sender: self)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Item" {
        let vc : ItemViewController = segue.destination as! ItemViewController



        vc.count = itemCount[selectedIndex]
        vc.image = foodImage[selectedIndex]
        vc.name = foodKind[selectedIndex]
这是ItemViewController变量:

var name : [String] = [""]   //this is the only way I can code it without getting an error.

var image = [UIImage]()   // lost here

var count : [String] = [""]   //this is the only way I can code it without getting an error.


override func viewDidLoad() {
    super.viewDidLoad()

    self.itemName.text = String(describing: name)

    self.itemImage.image = UIImage(named: image) //not working!!! Cant seem to find the right code for this from converting UIIMAGE to String. I am just lost!

    self.itemCount.text = String(describing: count)
下面是我得到的“Image”变量错误代码的屏幕截图:

当我忽略图像时(因为我无法计算代码),我构建了应用程序,我得到了以下结果:


您的问题似乎很简单:

var image = [UIImage]()   // lost here
这是一组图像

建议单个项目,您想要单个图像吗

var image:UIImage!
警告,如果使用时图像为零,将崩溃。(可选使用UIImage?但必须展开)

因此,在表视图控制器中

vc.image = foodImage[indexPath.section][indexPath.row]
将按预期进行操作。无需使用:

self.itemImage.image = UIImage(named: image) //not working!!! Cant seem to find the right code for this from converting UIIMAGE to String. I am just lost!
(这是不正确的,因为您正试图使用图像数组传递到从文件加载图像的字符串参数。)

只需使用:

self.itemImage.image = self.image
因为它们都是图像类型

在单元格中执行正确的操作,并在数组中为数组编制索引以获取图像,因此在此处执行相同的操作,并在局部视图控制器中使用单个图像类型。这样,如果单击beef,将发送beef的图像

作为一个旁白,考虑使用一个数据模型类型作为配方。

class Ingredient {
var name:String = ""
var image:UIImage! = nil
var count:Int = 0
var calories:Int = 0

public init(name: String, image: UIImage, count: Int, calories: Int) {
self.name = name
self.image = image
self.count = count
self.calories = calories
}
}
然后你可以说:

let ingredients = [Ingredient("Beef", beefImage, 100, 350), Ingredient("Onion", onionImage, 1, 20)]

或诸如此类,谢谢您抽出时间!我正在尝试使用一组图像,而不仅仅是一个图像…项目详细信息需要显示多个图像?或者你的意思是tableview有一组图像,但是item view只显示一个图像?如果你看我附加的tableview图片,我希望它显示我在itemviewcontroller中点击的表行的图片。修改了我的答案,最重要的问题:如果你点击牛肉,细节页面是否需要只显示牛肉?是的!我明白你的意思了!如果我点击牛肉,我希望它在图片上方有“绞牛肉”的名称,在图片下方有服务。这适用于我挑选的其他每一件物品。这有意义吗?Valerino错误很常见,您无法在image init中传递UIImage数组,预期参数是字符串值。因此,将图像名称作为字符串传递(例如-:“MyImage.png”)。执行类似于-:self.itemImage.image=UIImage(名称:image[0])的操作,并从特定索引中获取字符串值,并使用包含图像名称或url的字符串数组代替UIImage数组。ex-:var image=[string]()。现在,在这个数组中保存资产中的图像名称,或者从服务器获取数据时,解析JSON并获取字符串URL,然后追加到数组中。