无法出列与标识符“costumeCell”细胞 - 必须注册标识的笔尖或类或在故事板
问题描述:
我试图创建一个简单的应用程序连接原型细胞,但:无法出列与标识符“costumeCell”细胞 - 必须注册标识的笔尖或类或在故事板
“NSInternalInconsistencyException”,原因:“无法出列具有标识符术语细胞 - 必须注册标识符的笔尖或一类或在故事板连接原型细胞”
代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CostumeCellView
cell.textLabel?.text = titles[indexPath.row]
return cell
}
个
屏幕:
类
标识
我不知道该怎么做。
答
您可能需要注册您的手机。它看起来像这样。
如果你在代码中使用电池类:
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
如果您使用的是电池用笔尖:
let nib = UINib(nibName: "MyTableViewCellNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "Cell")
如果你使用,你需要一个故事板确保您的原型单元格安装正确。这其实答案有明确定义的https://*.com/a/14939860/563381
https://developer.apple.com/reference/uikit/uitableview/1614937-register
答
我希望你要加载从方法顾名思义的XIB.As,出列方法的原型细胞,只是出列的单元格,如果它已经创建并在内存中提供。你必须创建时出队没有找到一个适合你,连同下面的方法让你的细胞,这就是我平时装载原型细胞做,你不需要在这里做的RegisterClass/registernib,
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? CostumeCellView
if cell == nil {
let topLevelObjects = NSBundle.mainBundle().loadNibNamed("MyCellNibFile", owner: self, options: nil)
for entry in topLevelObjects {
if entry.reuseIdentifier == "Cell" {
cell = entry as? CostumeCellView
}
}
}
cell.textLabel?.text = titles[indexPath.row]
return cell
您的手机已连接,但您的桌面视图设置在哪里?它在代码或故事板中。如果它在代码中需要'tableView.registerCell(_ :)' –
或者如果你的单元格是一个笔尖,那么你还需要注册该笔尖。 –
[dequeueReusableCellWithIdentifier:forIndexPath:中断言失败的可能重复](http://*.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath) –