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

时间:2017-11-01 02:50:17

标签: ios arrays swift uitableview tableview

所以我最近两天都花在这上面,我可以肯定地说我被困了。

所以,我创建了一个链接到自定义单元格的tableview,我已经设置了一个项目数组,并且在构建它时每个部分内的顺序正确,一切都很好,花花公子但是我不能看到通过信息!哈哈。我一直都有错误!我将尝试尽可能详细,因为我是新来的(和一个3周大的编码员#beginner)并且不确定我可以附加多少信息但是这里什么都没有:

这些是我的数组和表函数:

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

Heres是我的" didselectrow"和"准备"功能:

    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"获取的错误代码的屏幕截图。变量:

enter image description here

当我遗漏图像时(因为我无法计算代码)我构建了应用程序并得到了这个:

enter image description here

This will give you a visual

1 个答案:

答案 0 :(得分:3)

你的问题似乎很简单:

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!

(这是不正确的,因为您正在尝试使用图像数组从文件传递到加载图像的STRING参数。)

只需使用:

self.itemImage.image = self.image

因为它们都是图像类型。

你在单元格中做正确的事情并索引数组中的数组以获取图像,所以在这里做同样的事情并在细节视图控制器中有一个单一的图像类型。这样,如果点击牛肉,就会发送牛肉图片。

另外,请考虑为您的食谱使用数据模型类型。

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)]

或等,