2017-11-01 20 views
1

これで最後の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) 

は、ここで私は「イメージ」変数の取得エラーコードのスクリーンショットです:

(私はのためのコードを理解することはできませんので)私は画像を除外

enter image description here

私はアプリを構築し、あなたの問題は、単純なようだ

enter image description here

This will give you a visual

+0

Valerinoエラーが非常に一般的ですが、あなたは画像のinitにUIImageの配列を渡すことができない、期待される引数は、文字列値です。文字列(例: "MyImage.png")として画像名を渡します。 - :self.itemImage.image = UIImage(image [0]という名前)を行い、特定のインデックスから文字列値を取得し、 UIImageはあなたのイメージ名またはURLを保持する文字列の配列を作る。 –

+0

ex-:var image = [String]()。この配列では、アセットにあるイメージ名を保存するか、サーバーからJSONを解析して文字列URLを取得して配列に追加します。 –

答えて

3

:私はこれを取得

var image = [UIImage]() // lost here 

これはイメージのARRAYです。

1つのアイテムを提案すると、1つの画像が欲しいですか?

var image:UIImage! 

使用時に画像がnilの場合、警告が発生します。 (代わりにUIImage?を使用しますが、アンラップする必要があります)。予想通り

Thererebyは、テーブルビューコントローラに..

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! 

(。あなたは、ファイルからロード画像の文字列パラメータに渡す画像の配列を使用しようとしているので、間違っています)

だけ使用しない:

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

かなど、

+0

お時間をありがとう!私は1つの画像だけでなく、一連の画像を使用しようとしています... –

+0

アイテムの詳細は複数の画像を表示する必要がありますか?またはtableviewに画像の配列があることを意味しますが、アイテムビューには1つの画像しか表示されませんか? –

+0

添付のtableview画像を見ると、itemviewcontrollerでタップしているテーブル行の画像が表示されます。 –

関連する問題