2017-03-02 18 views
2

類似的問題被問到here但這並不能解決這個問題。我在ViewController中添加了tableView。使用它的dataSource和Delegate擴展了類,併爲其添加了所需的方法。
然後我在此表(不爲它的單獨的.xib)製成的原型細胞和由類爲該TableViewCell並收集@IBOutlet致命錯誤:在UITableViewCell中解包可選值時出現意外的nil

@IBOutlet weak var titleOfAccount: UILabel! 
    @IBOutlet weak var lastModified: UILabel! 

    @IBOutlet weak var accountImage: UIImageView! 
    @IBOutlet weak var cellView: UIView! 

然後,在表視圖cellForRowAt方法時,我寫

cell.titleOfAccount.text = "Hello World" 
cell.lastModified.text = "Last Modified: 21 May 2017" 

並運行它崩潰的代碼與此錯誤。

fatal error: unexpectedly found nil while unwrapping an Optional value

我搜索在這裏和那裏後,我才又回到tableViewCell類,並改變!?和更新代碼cellForRowAt爲:

cell.titleOfAccount?.text = "Hello World" 
cell.lastModified?.text = "Last Modified: 21 May 2017" 

現在,當我運行該程序成功運行但在tableView中沒有出現細胞,我也實施了該方法:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3; 
    } 

以下是我的cellForRowAt方法的完整代碼:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell: AccountsTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! AccountsTableViewCell 

     cell.backgroundColor = UIColor.clear 
     cell.clipsToBounds = true 

     cell.cellView.layer.shadowColor = UIColor.black.cgColor 
     cell.cellView.layer.shadowOpacity = 1 
     cell.cellView.layer.shadowOffset = CGSize.zero 
     cell.cellView.layer.shadowRadius = 10 

     cell.titleOfAccount.text = "Hello World" 
     cell.lastModified.text = "Last Modified: 21 May 2017" 
     cell.accountImage.image = UIImage(named: "fb") 

     return cell 
    } 

P.S.我也viewDidLoad

enter image description here enter image description here

+0

let cell = tableView.dequeueReusableCell(withIdentifier:cellReuseIdentifier,for:indexPath)as! AccountsTableViewCell –

+0

nope仍然是相同的錯誤! –

+0

如何聲明可重用標識符? –

回答

3

從你viewDidLoad()刪除以下行(代碼),因爲你已經連接從故事板你的手機。

self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) 

還爲身份檢查員(屬性檢查器的左側)共享快照。

+1

是的!我完全忘了這個... #SwiftBeginners –

0

加入self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)試試這個

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "yourcellidentifier", for: indexPath) as! AccountsTableViewCell 
      // add your code 
    } 
+0

沒有仍然相同:'致命錯誤:意外地發現零,而解包可選值' –

+0

檢查你的單元格標識符與故事板請它應該是「yourcellidentifier」 –

+0

剛剛檢查它是相同的兩端'accountCell' –

相關問題