Swift 4:将数据从 Tableview 传递到 ViewController
Posted
技术标签:
【中文标题】Swift 4:将数据从 Tableview 传递到 ViewController【英文标题】:Swift 4: Passing Data from Tableview to ViewController 【发布时间】: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)
这是我为“图像”变量获取的错误代码的屏幕截图:
当我忽略图像时(因为我无法计算代码),我构建了应用程序并得到了这个:
This will give you a visual
【问题讨论】:
Valerino 错误很常见,你不能在图像初始化中传递 UIImage 数组,预期的参数是字符串值。所以将图像名称作为字符串传递(例如:“MyImage.png”)。做类似于 -: self.itemImage.image = UIImage(named: image[0]) 并从特定索引获取字符串值,而不是 UIImage 数组,而是创建包含图像名称或 url 的字符串数组。 ex-: var image = [String]() 。现在在这个数组中保存您在资产中拥有的图像名称,或者如果您从服务器解析 JSON 并获取字符串 URL 获取数据,并附加到数组中。 【参考方案1】:你的问题看起来很简单:
var image = [UIImage]() // lost here
这是一组图像。
建议单件商品,您想要单张图片吗?
var image:UIImage!
警告,如果使用时图像为 nil,则会崩溃。 (替代使用 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)]
等等,
【讨论】:
感谢您的宝贵时间!我正在尝试使用一组图像而不仅仅是一个图像...... 商品详情需要显示多张图片?还是您的意思是 tableview 有一组图像,但 item view 只显示一个图像? 如果你看一下我附加的tableview图片,我希望它显示我在itemviewcontroller中点击的表格行的图片。 修改了我的答案,最重要的问题:如果你点击牛肉,详细页面是否需要只显示牛肉? 是的!我明白你在问什么!如果我点击牛肉,我希望它在图片上方有“碎牛肉”的名称,在图片下方有服务。这适用于我选择的所有其他项目。这有意义吗?以上是关于Swift 4:将数据从 Tableview 传递到 ViewController的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 SWrevaelviewController swift 3 将数据从 tableview 的特定行传递到另一个视图控制器
将数据从 tableview 传递到另一个 tableview
将数据从 TableView 发送到 DetailView Swift
Swift iOS - 如何将直接从 TableViewCell 的子类中获取的值传递给 TableView 的 didSelectRow?