我知道这个问题已被问了很多次,但我检查了各种答案中的所有可能的问题,它仍然无法正常工作。致命错误:意外地发现零,而解包可选值(刷卡导航)
我想自定义单元格添加到表格视图,但出现此错误消息,并且应用程序崩溃:
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
摆脱你隐式解开的选项并强制解包。如果你需要[so]的帮助,你需要发布你的实际代码,而不是下载你的代码的zip链接,这绝对是你的代码,绝对不是病毒,绝对不会腐烂。 – nhgrif
谢谢,我要在这里发布代码,只是检查包括故事板在内的所有内容。 – TheoF