-1

In the cellForRowAtIndexPath method, I have created a custom cell. The cell has a label to display project name. After assigned value to the label, it shows nil value when print. so that label could not display in the table view.

let cell = self.LeadsListTable.dequeueReusableCell(withIdentifier:"LeadsID", for: indexPath) as! LeadsCell

    if let leadsDict: NSDictionary = self.leadsArray[indexPath.row] as? NSDictionary
    {
       cell.project_title?.text = (leadsDict.object(forKey: "project_title") as! String)
    }

This way i used to display the value of the label

po print(cell.project_title?.text) (or) print(cell.project_title?.text)
A.Yesuvin
  • 25
  • 6

2 Answers2

1

If the print statement returns nil, that could only mean cell.project_title is nil, since neither cell nor .text are optionals. Ensure that you initialize the .project_title property before you try setting its .text property.

Attackfarm
  • 512
  • 3
  • 9
0

@A.Yesuvin

put this line above your optional binding (if let) statement

cell.project_title?.text = "test_text"

If then when you print print(cell.project_title?.text) and it's "test_text", then you know you're not allowed to do this : self.leadsArray[indexPath.row] as? NSDictionary

If that was the case then try do what @vadian said and use native Swift Dictionary ([String:Any]).

If the code in the if statement actually runs, then double check if the key "project_title" is actually in the dictionary.

If thats the case, then ...well, fix it.

Also just a hint: Continue to use 'optional binding' instead of force unwrapping/ force cast cause you will eventually run into errors doing this: (leadsDict.object(forKey: "project_title") as! String For all we know, This might just be where the "error/nil" lays.

PhillipJacobs
  • 2,337
  • 1
  • 16
  • 32