-1

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 :)

pierreafranck
  • 445
  • 5
  • 19
  • 1
    You probably didn't register your cell, but there isn't enough information here to actually provide an answer. – jscs Aug 23 '17 at 12:45
  • @JoshCaswell Register my cell? – pierreafranck Aug 23 '17 at 12:48
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – FelixSFD Aug 23 '17 at 18:48

1 Answers1

1

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

Leo
  • 68
  • 9
  • The problem is not here, tableView gestion is correct, when I remove the two lines, it works perfectly. I just want to add a Label to print more informations in Cell. – pierreafranck Aug 23 '17 at 13:02
  • When the app crash enter : "po array[indexPath.section].datas[indexPath.row].name" (in the debug console) to see the result – Leo Aug 23 '17 at 13:04
  • thats correct dude, the problem is not in my array. The problem come from the Cell class. – pierreafranck Aug 23 '17 at 13:05
  • I can't find from where it comes, if you're sure the datasource is 100% correct it should work... just a little tip : instead of using `let cellText = listTableView.cellForRow(at: indexPath) as! Cell` that use a forceCast , you should use something like : `guard let cellText = listTableView.cellForRow(at: indexPath) as? Cell else { return UITableViewCell() } ` – Leo Aug 23 '17 at 13:13
  • That works now, but it's printing nothing into the Label. – pierreafranck Aug 23 '17 at 13:17
  • As Josh said , it seems your cell isnt registred , go in the storyboard , click on the cell you have and watch that in the attribute inspector in the field "Class " you have "Cell" (<- the name of your cell's class) – Leo Aug 23 '17 at 13:25
  • Ok the last thing I can say to (maybe) help you is that in the method "cell for row at indexpath..." I've always seen this : ` let myCell = tableview.dequeueReusableCell(withIdentifier : "myCellIdentifier", for indexPath) as! MyCellClass ` in your case, you should set an Identifier (in the attribute inspector) and it should be like that now that you use the "guard" statement that I gave you : `guard let cellText = listTableView.dequeueReusableCell(withIdentifier : [the identifier you'veput],for indexPath) as? Cell else { return UITableViewCell() }` – Leo Aug 23 '17 at 13:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152648/discussion-between-pierreafranck-and-leo). – pierreafranck Aug 23 '17 at 13:58