これで最後の2日間を費やしているので、私は積極的に私が立ち往生していると言うことができます。スウィフト4:TableviewからViewControllerへのデータの受け渡し
カスタムセルにリンクされたテーブルビューを作成しました。ビルド時に各セクション内のアイテムの配列が正しい順序で設定されていますが、そのすべてが上品でダンディーですが、情報を渡すことはできません!ハハ。私は間違いを続けている!私はここに新しい(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エラーが非常に一般的ですが、あなたは画像のinitにUIImageの配列を渡すことができない、期待される引数は、文字列値です。文字列(例: "MyImage.png")として画像名を渡します。 - :self.itemImage.image = UIImage(image [0]という名前)を行い、特定のインデックスから文字列値を取得し、 UIImageはあなたのイメージ名またはURLを保持する文字列の配列を作る。 –
ex-:var image = [String]()。この配列では、アセットにあるイメージ名を保存するか、サーバーからJSONを解析して文字列URLを取得して配列に追加します。 –