46752

cannot populate tableview with just the name

Question:

I need to populate my tableview with just the name of the Categories. When I populate the tableview it shows this...

Category(name: "Sandwiches & Burgers", items: [])

Heres where I append all of my information...

let sortedKeys = newArray.sorted{$0.0 < $1.0} 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]) menuArray = [a_w]

and then my class

struct Item { let name: String let carbs: Int } struct Category { let name: String let items: [Item] } struct Restaurant { let name: String let categories: [Category] }

Here's where I populate...

var currentRestaurant:Restaurant! var menuArray:[Restaurant] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var array = currentRestaurant.categories print(array) let currentCell = tableView.dequeueReusableCell(withIdentifier: "cell")! currentCell.textLabel?.text = String(describing: array[indexPath.item]) return currentCell } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return currentRestaurant.categories.count }

Answer1:

Looking at your data source cellForRowAt is supposed to be:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let category = currentRestaurant.categories[indexPath.row] cell.textLabel!.text = category.name return cell }

But at some point before reloading the table view you have to set currentRestaurant to menuArray[0]

<hr />

Actually the usual way to handle this kind of data source is to displays the restaurants in sections.

Please declare the data source array to some more descriptive name than menuArray

var restaurants = [Restaurant]()

Then return the number of restaurants in numberOfSections

override func numberOfSections(in tableView: UITableView) -> Int { return restaurants.count }

and the number of categories in numberOfRowsInSection

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let restaurant = restaurants[section] return restaurant.categories.count }

and cellForRow is

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let restaurant = restaurants[indexPath.section] let category = restaurant.categories[indexPath.row] cell.textLabel!.text = category.name return cell }

With this way you don't need currentRestaurant at all.

In viewDidLoad populate the data source array and reload the table view

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: []) restaurants = [Restaurant(name: "A&W", categories: [beverages, chips_fries, desserts, other, sandwiches_burgers, sides])] tableView.reloadData() }

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
  • Create tags in edit text? [closed]
  • Not populating tableview with structure array
  • Can Android renderscript run on GPU?
  • Detect substrings using regex in python [duplicate]
  • RStudio - Shiny - Error “Operation not allowed without an active reactive context”
  • selection list become empty when datasource is updated - datatable materil2
  • Get value in a row on click and pass it to popup
  • How are sin and cos implemented hardware wise?
  • Pandas DataFrame to List of Dictionaries
  • Physical core and Logical cores on diffrent cpu AMD/Intel
  • Adjust cell width in grouped UITableView
  • Multiplication with constant - imul or shl-add-combination
  • Order an array based on one property [duplicate]
  • PopOver auto adjust placement
  • Error: iisexpress.exe - Assertion failed
  • 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?
  • PHP & MySQL - list query results under heading and subheading
  • Double plus sign for the last line in git-diff of merge
  • How to run npm command on azure app service after deployment success by VSTS?
  • Implementing different layouts for RecyclerView but getting class cast erro on onBindViewHolder
  • Renamed directory, now SVN not allowing me to commit
  • How to set my Name in smack?
  • Geolocation method to see if user in specific area
  • Django foreign key drop down
  • MS Access - How to change the linked table path by amend the table
  • uniform generation of points on 3D box
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined