2016-07-31 88 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

相關問題