uynva uynva - 5 months ago 9
iOS Question

TableView Cell Error - iOS/Swift

i have this error when run the tableView cellForRowAtIndexPath:

fatal error: unexpectedly found nil while unwrapping an Optional value

I do not understand, I have correctly assigned the identifier id but by mistake ..

This is my code:

When override the fun cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("CellDataflow") as UITableViewCell

cell.textLabel.text = "This is a title."
return cell
}


And here is where i call the view:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

….more code…
self.navigationController?.pushViewController(MainTVC(), animated: true)

}


N.B. => I call this way the new view because I have a custom cell, and I could not call it from the storyboard.

Thanks

EDIT: The error in in the line:


let cell = tableView.dequeueReusableCellWithIdentifier("CellDataflow")
as UITableViewCell

Answer

Don't you also need the index path ?

So:

let cell = tableView.dequeueReusableCellWithIdentifier("IDENTIFIER", forIndexPath: indexPath) as UITableViewCell
Comments