2016-07-31 90 views
-2

我知道这个问题已被问了很多次,但我检查了各种答案中的所有可能的问题,它仍然无法正常工作。致命错误:意外地发现零,而解包可选值(刷卡导航)

我想自定义单元格添加到表格视图,但出现此错误消息,并且应用程序崩溃:

Fatal error: unexpectedly found nil while unwrapping an Optional value

这里是我加的表视图控制器的代码视图:

import UIKit 

class VC0: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var myTableView: UITableView! 

var names = ["Artist name", "Artist Name", "Artist name", "Artist name", "Artist name", "Artist name"] 
var cities = ["City", "City", "City", "City", "City", "City"] 
var images = [UIImage(named: "avatar"), UIImage(named: "avatar1"), UIImage(named: "avatar2"), UIImage(named: "avatar3"), UIImage(named: "avatar4"), UIImage(named: "avatar5")] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.myTableView.registerClass(CustomCell.self, forCellReuseIdentifier: "cell") 
    self.myTableView.dataSource = self 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return names.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.myTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell 

    cell.photo.image = images[indexPath.row] 
    cell.name.text = names[indexPath.row] 
    cell.city.text = cities[indexPath.row] 


    return cell 
} 

} 

该线路上出现的错误:

cell.photo.image = images[indexPath.row] 

这里是项目,如果你想看看完整的代码:http://dl.dropboxusercontent.com/s/frhaniuw5e36xej/swipeViews%20-%20copie.zip?dl=0

+1

摆脱你隐式解开的选项并强制解包。如果你需要[so]的帮助,你需要发布你的实际代码,而不是下载你的代码的zip链接,这绝对是你的代码,绝对不是病毒,绝对不会腐烂。 – nhgrif

+0

谢谢,我要在这里发布代码,只是检查包括故事板在内的所有内容。 – TheoF

回答

2

你使用registerClass覆盖你创建的故事板原型单元格。从UITableView文档:

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry.

相反,你创建的CustomCell新的空白,实例,它没有你的网点挂钩。删除该行,并应该工作。

+0

它工作得很好,非常感谢。 – TheoF

相关问题