1
votes

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

3

3 Answers

0
votes

Don't you also need the index path ?

So:

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

I am not a Swift expert but I checked the documentation, the method returns an optional value:

func dequeueReusableCellWithIdentifier(_ identifier: String) -> AnyObject?

Perhaps it is returning nil, try using as? instead of as and check for nil.

-1
votes
let cell = tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as UITableViewCell

I think this will help you...