DanCode
DanCode

Reputation: 673

Unable to populate data into table view

This is Swift 4. I was not able to populate the data (in the information array) into the table view. I am expecting the strings "Name", "Date of Birth", "Gender", "Phone", "Email" to show up in the table view but nothing is showing up.

I have entered "ProfileInfo" as my tableViewCell identifier.

Thanks.

let information = ["Name", "Date of Birth", "Gender", "Phone", "Email"]

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileInfo", for: indexPath) as! OTProfileTableViewCell
        cell.label.text = information[indexPath.row]
        return cell
    }

No data is being populated into table view

Upvotes: 1

Views: 143

Answers (1)

Morse-Code
Morse-Code

Reputation: 76

There are some things the OP didn't include. I assume you are using a UIViewController subclass with a UITableView as a subview (since there is another view above the table and this is not as straightforward with a UITableViewController).

Please try the following if they are not already being done:

  1. Make sure your view controller is assigned as the tableView.dataSource. You can set this in viewDidLoad or in interface builder.
  2. Declare conformance to the UITableViewDataSource protocol in your view controller. Preferably in an extension that contains implementation of all of the required methods of the protocol.

Is tableView(_:cellForRowAt:) being called and is the cell you are grabbing the correct type? Use a breakpoint in this method to inspect. If the breakpoint isn't hit you are likely not setting the data source of the table properly.

Set a breakpoint in tableView(_:numberOfRowsInSection:) as well. This method should always be executed (as long as you have at least 1section).

Upvotes: 4

Related Questions