非常简单的代码:
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被正确地连接在故事板的原型单元格中。我为什么要犯这个错误?
发布于 2014-07-18 20:59:16
当您在代码中创建视图时,它的IBOutlet属性不能正确地连接起来。你想要从dequeueReusableCellWithIdentifier得到的版本
let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell发布于 2014-10-14 00:08:58
如果您使用的是故事板,请确保在文件开始时没有这一行:
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")它将覆盖故事板,从而忽略故事板中的出口链接。
发布于 2015-04-08 01:30:17
我收到这个错误是因为我没有在自定义单元格的童话板中写入标识符。

还请确保它与以下代码相匹配:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell
...
}https://stackoverflow.com/questions/24833391
复制相似问题