Todd M. Foughty
Todd M. Foughty

Reputation: 11

Adding data from an Array to a tableview

I have an iOS tabbed application with three tabs. The first view controller calculates your one rep max bench press. When you press the calculate button to calculate the results, it also populates a table on the second view controller. Within the table it displays the 60%, 65%, 70%, 75%, 80%, 85%, 90%, and 95% of the one rep total.

What I cannot figure out is how to add text to each line of the table saying "60% of your one rep max is:" and then populate the data from the array.

Is there a way to statically add text to a table, and then populate it with data from an array?

I have attached a picture of the table:

image

Upvotes: 0

Views: 75

Answers (1)

Cosmin
Cosmin

Reputation: 716

You can learn how to achieve this from this video: https://www.youtube.com/watch?v=N8h8FH3c7TU

The main ideas are:

  1. Your view needs to extend UITableViewDataSource and UITableViewDelegate and

    tableView.delegate = self tableView.dataSource = self

  2. You should set how many rows are in tableView through

    func numberOfRowsInTableView(_ tableView: NSTableView) -> Int

  3. You should set how many section are:

    func numberOfSectionsInTableView(_ tableView: UITableView) -> Int

  4. You should say what type of cell you want to display and init it with data from your array:

    func tableView(_ tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

And when you want to reload data just reload the array that use for create tableViewCell and call tableView.reloadData()

Upvotes: 1

Related Questions