![](/img/trans.png)
[英]App crash when MFMessageComposeViewController is initialisation
[英]App crashed on cell initialisation
這是我的單元格的自定義類:
class showSugTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: .Default, reuseIdentifier: "showSug")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
let categoryLbl = UILabel()
categoryLbl.frame = CGRectMake(20, 10, 100, 20)
categoryLbl.text = "Text"
contentView.addSubview(categoryLbl)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
這是來自cellForRowAtIndexPath
的代碼:
let cell = tableView.dequeueReusableCellWithIdentifier("showSug") as! showSugTableViewCell
return cell
問題是我的應用程序在單元初始化時崩潰,日志顯示:
致命錯誤:解開Optional值時意外發現nil
我該如何解決?
編輯:
我將代碼更改為此:
let cellTemp = self.sugTableView.dequeueReusableCellWithIdentifier("showSug")
var cell = cellTemp as? showSugTableViewCell!
if cell == nil {
self.sugTableView.registerNib(UINib(nibName: "showSugTableViewCell", bundle: nil), forCellReuseIdentifier: "showSug")
self.sugTableView.registerClass(showSugTableViewCell.classForCoder(), forCellReuseIdentifier: "showSug")
cell = showSugTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "showSug")
}
return cell!
但是,該應用程序現在在return語句上崩潰了。
請嘗試以下案例以確認問題-我堅信以下兩個問題可能是您的問題
if let retCell = tableView.dequeueReusableCellWithIdentifier("showSug") {
if let cell = retCell as? showSugTableViewCell {
//use the cell object
}else {
//1. cell may not be of type showSugTableViewCell so filing t convert
//If it is the case check in identifier you set in nib
}
}else {
//2. tableView.dequeueReusableCellWithIdentifier may not be returning for "showSug"
//Check class type it return table cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.