ios - Passing data from a viewcontroller into tableviewCell


Keywords:ios 


Question: 

I am trying to get the Json data to show up in a table view cell, I have no value showing up. Please let me know what I am doing wrong. My tableview is linked Correctly to my viewcontroller(both as datasource and delegate).

Note: I havent called self.tableview.reloadData() anywhere as of now. If this is what needs to be done, Please tell me where I have to call it. I tried on viewdidload() it didnt work.

//json data 


if let mydata = data {
                            do{
                                let myJson = try JSONSerialization.jsonObject(with: mydata, options: .mutableContainers) as AnyObject
                             if   let articlesFromJson=myJson["account"] as! NSDictionary?{
  self.points = articlesFromJson["Points__c"] as! Double
  self.tier = articlesFromJson["Member_Tier__c"] as! String

//My destination VC is called HomeViewController
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
               if segue.identifier == "HomeViewController" {
            if let destVc = segue.destination as? HomeViewController {
               destVc.pointrs = self.points
               destVc.tiers = self.tier
            }
        }

   }

//tableview 

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MemberInfoTableViewCell
                cell.points.text = String(pointrs)
        cell.tier.text = tiers
               return cell
    }

UPDATE : I just redid it, and it works. I must have missed something tiny! And The code seems to be correct. I also added a navigation controller so the segue code changed, nonetheless. It works. Thank you!


3 Answers: 

1) Make Sure your variable "pointrs" is initialized 2) you do not need to call table.relaod() when you are coming on the table view controller for the first time 3)make sure you have pass correct count in below delegate

override func tableView(_ tableView: UITableView, numberOfRowsInSection      section: Int) -> Int {
    return fruits.count //Array Count
} 
 

Try this,, pass the values through a function to the "TableViewCellClass" you created and then , set the values of labels from there...

 

Make sure you indeed pass data into your table view controller because your table view controller looks okay. When you pass data, your code is

if segue.identifier == "HomeViewController" {
    if let destVc = segue.destination as? HomeViewController {
       destVc.pointrs = self.points
       destVc.tiers = self.tier
    }//Your code can ends up here
}//Your code can ends up here

See my comments from the code. If any of these if statements are false, you are not passing data. So first put a break point here to figure out if you are actually passing data or not.

If you are indeed passing data, put a break point in your cellForRow method to again make sure both your pointrs and tiers are valid with correct value