Table View content in cell is not displayed

Hey there,


I want to create a TableView displaying datas the user created in the View before.

Just for testing I tried to set Strings manually so that I know the TableView is working correctly.


In storyboard I created a TableViewController with a TableView and a Cell. The Cell has the style "Subtitle" and the Indentifer "Cell".


The code in the matching class file is the following:

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

        // Configure the cell...
        cell.textLabel?.text = "Test"
        cell.detailTextLabel?.text = "Test2"

        return cell
    }


Anyone knows what I did wrong?


Best regards

Accepted Reply

hi,


we'd have to see more code, but the questions that need answering right away are:

  1. is your ViewController set as the delegate and datasource for the TableView?
  2. what's your definition for
    func numberOfRows(inSection: Int) -> Int

without these, no rows will be shown in the UITableView.


you'd probably also eventually want to add the code for

func numberOfSections(in tableView: UITableView) -> Int

(although this defaults to 1)


hope that helps,

DMG

Replies

hi,


we'd have to see more code, but the questions that need answering right away are:

  1. is your ViewController set as the delegate and datasource for the TableView?
  2. what's your definition for
    func numberOfRows(inSection: Int) -> Int

without these, no rows will be shown in the UITableView.


you'd probably also eventually want to add the code for

func numberOfSections(in tableView: UITableView) -> Int

(although this defaults to 1)


hope that helps,

DMG

Another thing to test is whether the func is called


So add a print inside

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

Thank you.

In fact numberOfSections(in tableView: UITableView) -> Int and tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int has had return value 0.

So I changed it to 1 and now it works 😄