Why isn't the value assigned to the cell label of the static table?

I create a Detail screen using a standard TableViewController. The TableView-Content option is Static Cells, the Table View Sell-Style option is Basic. The label is used by default, not custom. With the help of this line of code below, I try to pass a value to the label:

Code Block
tableView.cellForRow(at: [0,0])?.textLabel?.text = item!.itemID

The value comes to the controller, I see it, but for some reason it is not displayed on the label and displays a nil in the debugger.

What could be the mistake?
Answered by Claude31 in 634159022
I tested, that works.

Are you sure you don't reload the table after setting the textLabel ?

Code Block
tableView.cellForRow(at: [0,0])?.textLabel?.text = item!.itemID

Where do you call this ?


Accepted Answer
I tested, that works.

Are you sure you don't reload the table after setting the textLabel ?

Code Block
tableView.cellForRow(at: [0,0])?.textLabel?.text = item!.itemID

Where do you call this ?


I tried to use both in the viewDidLoad() or in the viewVillAppear() methods, adding tableView.reloadData(). It still doesn't work.

Could you show me your full code?
I did the test in existing code, in an IBAction that changes the cell height.

I just try to set the labelText of cell 0,0 to "Some test"
tableForItem2 is declared as IBOutlet

When I call cellForRow after reloadData(), I get cell label changed
Code Block
@IBAction func changeCellHeight(_ sender: UIButton) {
highCells = !highCells // Keeps state of cell height
for iCell in 0..<numberOfCells {
rowHeights[iCell] = highCells ? 100 : 44
}
tableForItem2.reloadData()
tableForItem2.cellForRow(at: [0,0])?.textLabel?.text = "Some test"
}


If I call reloadData() after cellForRow, I do not get cell changed

Code Block
@IBAction func changeCellHeight(_ sender: UIButton) {
highCells = !highCells // Keeps state of cell height
for iCell in 0..<numberOfCells {
rowHeights[iCell] = highCells ? 100 : 44
}
tableForItem2.cellForRow(at: [0,0])?.textLabel?.text = "Some test"
tableForItem2.reloadData() // That reloads the original label, hiding "Some test"
}


Thank you very much! It looks unexpected, but it works

Could you please explain to me a little why the tableView.reloadData() method is called here before passing the value to the label? It is usually called after the changes in the cell.
Why isn't the value assigned to the cell label of the static table?
 
 
Q