Ios Swift自定义单元格不';t在其中实例化一个标签!!致命错误:隐式展开可选值时意外发现nil
我有一个包含多个单元格的tableView,我可以选择这些单元格,但其中不显示label.textIos Swift自定义单元格不';t在其中实例化一个标签!!致命错误:隐式展开可选值时意外发现nil,ios,swift,uitableview,tableview,cell,Ios,Swift,Uitableview,Tableview,Cell,我有一个包含多个单元格的tableView,我可以选择这些单元格,但其中不显示label.text override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ShowDeviceCell{
cell.setEntry(deviceName: devices_names[indexPath.row])
return cell
}
let cell = ShowDeviceCell()
cell.setEntry(deviceName: devices_names[indexPath.row])
return cell
}
这是我的手机定义:
class ShowDeviceCell: UITableViewCell {
@IBOutlet weak var deviceCell: UILabel! = {
let lbl = UILabel()
lbl.textColor = .black
lbl.font = UIFont.boldSystemFont(ofSize: 16)
lbl.textAlignment = .left
return lbl
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// addSubview(deviceCell)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setEntry (deviceName : String){
if let cellLabel = self.deviceCell{
cellLabel.text = deviceName
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
每当我尝试使用cell.deviceCell时,错误都是一样的。我遇到了此错误致命错误:在隐式展开可选值时意外发现nil。
谢谢你的帮助。成功了,我删除了寄存器和init方法。并在情节提要中添加单元格的正确标识符。谢谢您的帮助。cellForRow中的
if let
表达式毫无意义。用力使细胞向下移动。如果它崩溃了,就会暴露出一个设计错误。那么插座中的初始值设定项是用来做什么的?如果它应该是一个插座,请在Interface Builder中进行设置。由于它不起作用,我正在测试不同的东西。但是谢谢你的帮助。