
Question:
I need to populate my tableView with an array of a structure. The first property of the structure is the name. This is what I tried...
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
}
Why won't it populate my tableView
Here is my class also...
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]
}
Answer1:In this line
let menuArray = [a_w]
you are creating a local variable menuArray
which is different from the property with the same name representing the data source array.
Omit let
menuArray = [a_w]
PS: Please use more descriptive variable names than a_w
.
From you code looks like you have two variables with same name.
<ol><li>One is declared at class level</li> <li>Another one is declared in viewDidLoad method like
let menuArray = [a_w]
</li> </ol>Just remove <strong>let</strong> from this which is declared in viewDidLoad.
It will look like this
menuArray = [a_w]
Answer3:Your menuArray containse only one item so only one row should be displayed with the restaurant name.
Try checking if you set the tableView delegate and dataSource properties
tableView.dataSource = self
tableView.delegate = self
Also the prototype cell with default label should have name titleLabel
Try accessing titleLabel
instead of textLabel
.