kavyajha1004
kavyajha1004

Reputation: 25

TableViewCell not being displayed in ViewController

I've come across a very strange issue suddenly in an app I'm building - the tableViewCells in my tableView are not showing up at all, even though the tableView methods are firing (I've checked using print statements within them). The app does not crash, but everything within the table view cell is just not showing up anymore, even though all data variables have been updated.

Below are the tableView methods I've used:

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

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

    let cell = carbonDetailsTable.dequeueReusableCell(withIdentifier: "CarbonDetailsCell") as! CarbonTableViewCell

    cell.itemName.text = selectedValidInputs[indexPath.row]
    cell.carbonValue.text = carbonValues[indexPath.row]
    cell.tip.text = tips[indexPath.row]
    cell.hashtag.text = tags[indexPath.row]

    return cell
}

Covering some questions that may be asked

  1. I have a Controller that conforms to UIViewController, UITableViewDelegate, UITableViewDataSource, so override methods are not required
  2. I have made sure the identifier for the CarbonTableViewCell (the custom UITableViewCell class) matches the one being used in .dequeueReusableCell method
  3. I have verified all the variable connections from the storyboard to the code
  4. I have not set the number of sections within the tableView, but the code had been working for a month without that too, so don't think the problem is associated with that.

I'm very new to coding altogether, so any help and feedback would be much appreciated!

Note that the page is just empty. Even the 'cellBg' variable below, which is just a view and has a background color, does not show up and neither does the app crash - which is a bit weird.

Edit: Since this is being suggested by everyone, just updating here. On printing the results, I get all values: When the input value is "apple" for example, all the values are shown:

count: 1
selectedValidInputs: ["apple (1 kg)"] 
carbonValues: ["550 g CO2, same as 22 plastic bags"] 
tips: ["Avoid wastage that adds unnecessary methane to the air with decomposition!"] 
tags: ["#very-low-carbon-impact"]

When I add carbonDetailsTable.register(CarbonTableViewCell.self, forCellReuseIdentifier: "CarbonDetailsCell") to viewDidLoad(), I get a crash with the msg shown in attached image

Upvotes: 0

Views: 99

Answers (3)

Lukáš Mareda
Lukáš Mareda

Reputation: 341

1) Try to implement also

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

2) Try to implement also

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return 40.0
}

And comment the estimatedRowHeight and rowHeight setters, to confirm if the cell's autolayout is not broken and table view is unable to determine row height. (estimatedRowHeight is basically used just for tableView indicator bar and estimating the content size of tableView)

Upvotes: 0

Divya Thakkar
Divya Thakkar

Reputation: 212

Print your Arrays which you are passing in tableview in CarbonDetailsViewController, and check data is exact as you passed in previous viewcontroller.

Upvotes: 0

Derp
Derp

Reputation: 147

Number of sections is equal 0, ‘selectedValidInputs’ array is empty or you have not placed all of the code that is initialising it with values.

Upvotes: 1

Related Questions