11942

Not populating tableview with structure array

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.

Answer2:

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.

Recommend

  • cannot populate tableview with just the name
  • I have a class which holds attributes for a menu item. I need to populate three JLists. How can I do
  • Get value in a row on click and pass it to popup
  • Using hidden input to preserve form contents
  • sed help: matching and replacing a literal “\\n” (not the newline)
  • Order an array based on one property [duplicate]
  • Merging two backbone collection and models into one object using underscore
  • How to avoid truncated bubbles when elasticX or elasticY
  • Why is git giving me a “deleted merge conflict” when both sides are deleted?
  • Replacing parts of expression in prolog
  • How to build a 64-bit .NET DLL, with 64-bit COM interop?
  • Double plus sign for the last line in git-diff of merge
  • Renamed directory, now SVN not allowing me to commit
  • How to set my Name in smack?
  • JQuery Auto-Complete: How do I handle modifications?
  • Removing Duplicate Geometries
  • pandas computation in each group
  • TFS - how do I sum child task hours to parent
  • Dynamically load css stylesheet and wait for it to load
  • Pass array from parent template to child template
  • Wrong row deleted from custom listview with spinner
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • converter json to two dimensional array
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Simple linked list-C
  • Check all values in string[] for length?
  • uniform generation of points on 3D box
  • How do I alternate colors in Flat List (React Native)
  • Typescript - Unable to get 'import' statement to function
  • How can I send an e-mail from a vbs script
  • Read text file and split every line in MSBuild
  • Align navbar back button on right side
  • Can Jackson SerializationFeature be overridden per field or class?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to push additional view controllers onto NavigationController but keep the TabBar?