2017-05-06 3 views
0

tableViewに構造体の配列を設定する必要があります。構造体の最初のプロパティは名前です。これは構造体配列でtableviewにデータを設定しない

var menuArray:[Restaurant] = [Restaurant]() 
override func viewDidLoad() { 
    super.viewDidLoad() 
let shake = Item(name: "Shake", carbs: 20) 
    let fries = Item(name: "Fries", carbs: 30) 

    let beverages = Category(name: "Beverages", items: [shake]) 
    let chips_fries = Category(name: "Chips & Fries", items: [fries]) 
    let desserts = Category(name: "Desserts", items: []) 
    let other = Category(name: "Other Menu Items", items: []) 
    let sandwiches_burgers = Category(name: "Sandwiches & Burgers", items: []) 
    let sides = Category(name: "Sides", items: []) 

    a_w = Restaurant(name: "A&W", categories: [beverages, chips_fries, desserts, other, sandwiches_burgers, sides]) 

    let menuArray = [a_w] 


} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell") 
    let currentRestaurant = menuArray[indexPath.row] 
    currentCell?.textLabel!.text = currentRestaurant.name 
    return currentCell! 
} 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return menuArray.count 
} 

は、なぜそれが私のtableViewここ

も私のクラスがある...これだけ

import Foundation 

struct Item { 
    let name: String 
    let carbs: Int 
} 

struct Category { 
    let name: String 
    let items: [Item] 
} 

struct Restaurant { 
    let name: String 
    let categories: [Category] 

} 
+0

のように見えるのだろうか? –

+0

はいそれは動作しませんでした – Johnd

+0

テーブルビューの代理人とデータソースを設定しましたか? –

答えて

0

let menuArray = [a_w] 

データ・ソース・アレイを表す同じ名前のプロパティは異なるローカル変数menuArrayを作成しています。

省略let

menuArray = [a_w] 

PS:a_wより説明的な変数名を使用してください。

+0

ありがとうございました – Johnd

-1

あなたmenuArrayのcontainse一つだけのアイテムを移入しません...私が試したものです1列はレストラン名で表示する必要があります。 あなたがのtableViewデリゲートとデータソースのプロパティ tableView.dataSource = self tableView.delegate = self

を設定している場合も、デフォルトのラベルを持つプロトタイプセルが名前titleLabel がtitleLabel代わりのtextLabelにアクセスしてみてください持っている必要がありますチェックしてみます。この行に

0

コードからは、同じ名前の2つの変数があるようです。

  1. 一つはクラスレベルで宣言されているもう一つのmenuArray = [a_w]

だけを削除しましょう

ようviewDidLoadメソッドで宣言され

  • があるこのからを聞かせviewDidLoadで宣言されています。

    それはあなたがそのデータを設定した後、テーブルビューを再ロードしようとしたことがあり、この

    menuArray = [a_w]