類似的問題被問到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
let cell = tableView.dequeueReusableCell(withIdentifier:cellReuseIdentifier,for:indexPath)as! AccountsTableViewCell –
nope仍然是相同的錯誤! –
如何聲明可重用標識符? –