Xcode 错误:无法将带有标识符 MealTableViewCell 的单元格出列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34730848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode error: unable to dequeue a cell with identifier MealTableViewCell
提问by demar
I have been following the apple tutorial hereand have come across an error:
我一直在这里关注苹果教程,但遇到了一个错误:
2016-01-12 09:34:32.909 FoodTracker[1812:124509] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier MealTableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
The error appears when the program is run, and the red highlighted line appears on the class line of AppDelegate.swift
程序运行时出现错误,AppDelegate.swift的class行出现红色高亮线
These are the lines of code I believe are causing the error, as I found out through breakpoints:
这些是我认为导致错误的代码行,正如我通过断点发现的那样:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "MealTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MealTableViewCell
// Configure the cell...
let meal = meals[indexPath.row]
cell.nameLabel.text = meal.name
cell.photoImageView.image = meal.photo
cell.ratingControl.rating = meal.rating
return cell
}
I have looked around online, and a lot of answers have said to ensure that the TableCell has an identifier, however mine does and the error still pops up.
我在网上环顾四周,很多答案都说要确保 TableCell 具有标识符,但是我的却这样做了并且错误仍然弹出。
Please let me know if I need to post any more info.
如果我需要发布更多信息,请告诉我。
Thanks in advance
提前致谢
回答by Jan
回答by demar
Just for the record, here's how I solved my issue:
仅作记录,以下是我解决问题的方法:
I took the current identifier that was in the attributes inspector, deleted it, pressed return and clicked away. After that, I clicked back into the identifier text box and re-typed the identifier and pressed return. I then saved the storyboard file and it worked when I ran it.
我获取了属性检查器中的当前标识符,将其删除,按回车键并单击离开。之后,我单击回标识符文本框并重新键入标识符并按回车键。然后我保存了故事板文件,当我运行它时它就可以工作了。
回答by Andrea Gorrieri
In my case I was taking the UITableViewCell from a separate xib file (i.e., not inserting the cell directly in the tableView in storyBoard) and I forgot to properly register the cell in the tableView in this way:
在我的例子中,我从一个单独的 xib 文件中获取 UITableViewCell(即,没有直接在 storyBoard 的 tableView 中插入单元格),但我忘记以这种方式在 tableView 中正确注册单元格:
self.tableView.register(UINib(nibName: "NAME_OF_THE_CELL_CLASS", bundle: nil), forCellReuseIdentifier: "REUSE_IDENTIFIER");
回答by Juanes30
Swift (5.0)
斯威夫特 (5.0)
I had the same problem but in my case the view was a xib, the way I solved it was:
我遇到了同样的问题,但在我的情况下,视图是一个 xib,我解决它的方法是:
Bundle.main.loadNibNamed("MealTableViewCell", owner: self, options: nil)?.first as! MealTableViewCell
the complete code would be the following:
完整的代码如下:
import UIKit
class MealTableViewCell: UITableViewCell {
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
}
}
extension NameViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("MealTableViewCell", owner: self, options: nil)?.first as! MealTableViewCell
return cell
}
}