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
的标识符被设置,但我仍然得到这个错误。
我还可以做些什么?
- 在Storyboard中select您的原型单元格
- alt + cmd + 4打开Attribute Inspector
- 将
CELL
写入Identifier
字段。
然后再试一次;)
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
为了你的问题。
尝试
let cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath) as! SingleEventCell