So I have been spending the last two days on this and I can positively say that I am stuck.
所以我最近两天都花在这上面,我可以肯定地说我被卡住了。
so, I created a tableview linked to a custom cell, I have an array of items already set and in the right order within each section when I build it, all that is fine and dandy BUT I CANNOT SEEM TO PASS INFORMATION! haha. I keep getting errors! I am gonna try to be as detailed as possible as i am new here (and a 3 week old coder #beginner) and not sure how much info I can attach but here goes nothing:
所以,我创建了一个链接到自定义单元格的tableview,我已经设置了一系列项目,并且在我构建它时,每个部分内的顺序正确,一切都很好,花花公子但是我不能看到通过信息!哈哈。我一直都有错误!我将尝试尽可能详细,因为我是新来的(和一个3周大的编码器#beginner)并且不确定我可以附加多少信息但是这里什么都没有:
these are my arrays and table functions:
这些是我的数组和表函数:
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 is my "didselectrow", and "preparesegue" functions:
Heres是我的“didselectrow”和“preparesegue”功能:
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]
This is the ItemViewController variables:
这是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)
Here's a screenshot of the error code I get for the "Image" variable:
这是我为“Image”变量获取的错误代码的屏幕截图:
When I leave out the Image (since I can't figure the code for) I build the app and I get this:
当我遗漏图像(因为我无法想出代码)时,我构建了应用程序,我得到了这个:
这会给你一个视觉
1 个解决方案
#1
3
Your problem seems simple:
你的问题似乎很简单:
var image = [UIImage]() // lost here
That's an ARRAY of images.
这是一个图像阵列。
Suggest for a single item, you want a single image?
建议单个项目,你想要一个单一的图像?
var image:UIImage!
Warning, will crash if image is nil when used. (alternate use UIImage? but must unwrap).
警告,如果使用时图像为零,则会崩溃。 (替代使用UIImage?但必须解开)。
Therereby, in the table view controller..
因此,在表格视图控制器..
vc.image = foodImage[indexPath.section][indexPath.row]
Will be behave as expected. No need to use:
将表现得如预期。无需使用:
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!
(Which is incorrect because you're trying to use an array of images to pass to the STRING parameter of load image from file.)
(这是不正确的,因为您尝试使用图像数组从文件传递到加载图像的STRING参数。)
Just use:
self.itemImage.image = self.image
As they are both image types.
因为它们都是图像类型。
You do the correct thing in the cell and index the array within the array to get the image, so do the same here and have a singe image type in your detail view controller. In that way if you click beef, the image for beef is sent.
你在单元格中做正确的事情并索引数组中的数组以获取图像,所以在这里做同样的事情并在细节视图控制器中有一个单一的图像类型。这样,如果点击牛肉,就会发送牛肉图像。
As an aside, consider using a data model type for your recipe.
另外,请考虑为您的食谱使用数据模型类型。
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
}
}
then you can do say:
然后你可以说:
let ingredients = [Ingredient("Beef", beefImage, 100, 350), Ingredient("Onion", onionImage, 1, 20)]
or etc,
#1
3
Your problem seems simple:
你的问题似乎很简单:
var image = [UIImage]() // lost here
That's an ARRAY of images.
这是一个图像阵列。
Suggest for a single item, you want a single image?
建议单个项目,你想要一个单一的图像?
var image:UIImage!
Warning, will crash if image is nil when used. (alternate use UIImage? but must unwrap).
警告,如果使用时图像为零,则会崩溃。 (替代使用UIImage?但必须解开)。
Therereby, in the table view controller..
因此,在表格视图控制器..
vc.image = foodImage[indexPath.section][indexPath.row]
Will be behave as expected. No need to use:
将表现得如预期。无需使用:
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!
(Which is incorrect because you're trying to use an array of images to pass to the STRING parameter of load image from file.)
(这是不正确的,因为您尝试使用图像数组从文件传递到加载图像的STRING参数。)
Just use:
self.itemImage.image = self.image
As they are both image types.
因为它们都是图像类型。
You do the correct thing in the cell and index the array within the array to get the image, so do the same here and have a singe image type in your detail view controller. In that way if you click beef, the image for beef is sent.
你在单元格中做正确的事情并索引数组中的数组以获取图像,所以在这里做同样的事情并在细节视图控制器中有一个单一的图像类型。这样,如果点击牛肉,就会发送牛肉图像。
As an aside, consider using a data model type for your recipe.
另外,请考虑为您的食谱使用数据模型类型。
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
}
}
then you can do say:
然后你可以说:
let ingredients = [Ingredient("Beef", beefImage, 100, 350), Ingredient("Onion", onionImage, 1, 20)]
or etc,