【发布时间】:2017-11-01 02:50:16
【问题描述】:
所以我过去两天一直在做这件事,我可以肯定地说我被困住了。
所以,我创建了一个链接到自定义单元格的表格视图,我已经设置了一组项目,并且在我构建它时在每个部分中以正确的顺序排列,这一切都很好,但我似乎无法传递信息!哈哈。我不断收到错误!我会尽量详细一点,因为我是新来的(以及 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
这是我的“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]
这是 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)
这是我为“图像”变量获取的错误代码的屏幕截图:
当我忽略图像时(因为我无法计算代码),我构建了应用程序并得到了这个:
【问题讨论】:
-
Valerino 错误很常见,你不能在图像初始化中传递 UIImage 数组,预期的参数是字符串值。所以将图像名称作为字符串传递(例如:“MyImage.png”)。做类似于 -: self.itemImage.image = UIImage(named: image[0]) 并从特定索引获取字符串值,而不是 UIImage 数组,而是创建包含图像名称或 url 的字符串数组。
-
ex-: var image = [String]() 。现在在这个数组中保存您在资产中拥有的图像名称,或者如果您从服务器解析 JSON 并获取字符串 URL 获取数据,并附加到数组中。
标签: ios arrays swift uitableview tableview