gpt4 book ai didi

ios - 应用程序在单元格初始化时崩溃

转载 作者:行者123 更新时间:2023-11-30 13:55:11 24 4
gpt4 key购买 nike

这是我的单元格的自定义类:

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

问题是我的应用程序在单元初始化时崩溃,日志显示:

fatal error: unexpectedly found nil while unwrapping an Optional value

我该如何解决这个问题?

编辑:

我将代码更改为:

   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!

但是,应用程序现在在返回语句时崩溃了。

最佳答案

尝试以下案例来确认问题 - 我坚信以下 2 个问题可能是您的问题

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
}

关于ios - 应用程序在单元格初始化时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com