pierreafranck pierreafranck - 1 month ago 10
Swift Question

xTableView.cellForRow(at: indexPath) as! Cell : unexpectedly found nil while unwrapping an Optional value

Considering this code

class Cell: UITableViewCell {
@IBOutlet weak var cellLabel: UILabel!
}


EDIT: Nothing in the viewDidLoad method for the moment.

I'm trying to put text in cellLabel in this other ViewController in cellForRowAt:indexPath method

let cellText = listTableView.cellForRow(at: indexPath) as! Cell
cellText.cellLabel.text = array[indexPath.section].datas[indexPath.row].name // ERROR HERE


When I run the app I have the following error message on the second line


fatal error: unexpectedly found nil while unwrapping an Optional value


Let me know what i'm doing wrong.

Thanks :)

Leo Leo
Answer Source

Does your "number of rows" method returns array[indexPath.section]. datas .count ?

If it's not the problem come from here because if you put something like

"number of rows in section = 10" but there is a section in your data source only containing 5 elements it will fail