swift and unexpectedly found nil while unwrapping an Optional value

折月煮酒 提交于 2020-01-03 06:41:11

问题


I have a class:

class ListOfEventsController: UIViewController, UITableViewDataSource, UITableViewDelegate {

and it contains a function:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("CELL") as! SingleEventCell

and when I run it - it causes an error that says

fatal error: unexpectedly found nil while unwrapping an Optional value

but when I go to my story board I see:

and when I click CELL I see in its properties:

and also:

so the identifiers for CELL are set up, but I'm still getting this error.

What else can I do?


回答1:


  1. In Storyboard select your prototype cell
  2. alt + cmd + 4 to open the Attribute Inspector
  3. Write CELL into the Identifier field.

Then try again ;)




回答2:


[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

For your problem.




回答3:


Try

let cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath) as! SingleEventCell


来源:https://stackoverflow.com/questions/35535791/swift-and-unexpectedly-found-nil-while-unwrapping-an-optional-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!