-1

When I run the following code, I get an empty table view. I have a TableViewController set up in my storyboard with content set to dynamic prototypes and with a prototype cell that has style set to basic in the attributes inspector. my TableViewCell has the identifier cell. Can anyone help me put the strings in the wines list in prototype cells?

code:

import UIKit

class ListTableViewController: UITableViewController {

    let wines = [ "Barbera", "Zinfandel", "Viognier" ]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {        
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
        return wines.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = wines[indexPath.row]

        return cell
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
vbh23
  • 1

1 Answers1

1

You need to reload the data of the table view:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.reloadData()
}

And if your table view contains only one section delete the entire method numberOfSections(in. The default value is 1.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks a lot. That fixed the problem. I'm building a todo list app and what I want the app to do is the following: when a user clicks on an item in the list (TableView), I want the text that is in the item (row) that he/ she selected to be sent to a text field in another ViewController. Do you know how I can achieve this? – vbh23 Mar 15 '17 at 23:56
  • Please read http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – vadian Mar 16 '17 at 05:37