swift和意外地发现零,同时展开一个可选值

我有一个class级:

class ListOfEventsController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

它包含一个function:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("CELL") as! SingleEventCell 

当我运行它 – 它会导致一个错误,说

 fatal error: unexpectedly found nil while unwrapping an Optional value 

在这里输入图像说明

但是当我去我的故事板时,我看到:

在这里输入图像说明

当我点击CELL我会看到它的属性:

在这里输入图像说明

并且:

在这里输入图像说明

所以CELL的标识符被设置,但我仍然得到这个错误。

我还可以做些什么?

  1. 在Storyboard中select您的原型单元格
  2. alt + cmd + 4打开Attribute Inspector
  3. CELL写入Identifier字段。

然后再试一次;)

在这里输入图像说明

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

为了你的问题。

尝试

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