非常简单的代码:
func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
return 5
}
func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
let cell: BookTableViewCell = BookTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BookCell")
println("ip: \(indexPath.row)")
cell.bookLabel.text = "test"
return cell
}在cell.bookLabel.text行中,我得到了以下内容:
fatal error: unexpectedly found nil while unwrapping an Optional valueBookTableViewCell的定义如下:
class BookTableViewCell: UITableViewCell {
@IBOutlet var bookLabel: UILabel
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}bookLabel被正确地连接在故事板的原型单元格中。我为什么要犯这个错误?
发布于 2020-02-26 01:44:21
Swift 5在同一个故事板中有A类和B类,B类包含表视图插座,当我试图将A类推到B类时,它崩溃了,显示tableview出口为零。
在A班,我像下面的代码那样进行导航。
let classBObj = ClassB()
self.navigationController?.pushViewController(classBObj, animated: true)然后,我意识到我的错误,并使用下面的代码,这是完美的工作。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let classBObj = storyboard.instantiateViewController(withIdentifier: "ClassB") as! ClassB
self.navigationController?.pushViewController(classBObj, animated: true)https://stackoverflow.com/questions/24833391
复制相似问题