我目前在我的 swift 文件中遇到一个错误,该主题的其他线程没有解决任何问题。
感谢我的 NewsTableViewController,我创建了一个 TableView ,我为我的自定义单元格创建了一个原型(prototype),我将其标识为 cell 在主 Storyboard 中。 Controller 是单独的,没有 segue 链接,就像这样显示的原型(prototype):
Controller on the right is my table view controller that bugs
但是当我执行应用程序时,当我单击启动实例化 NewsTableViewController 的代码片段的按钮时,出现错误
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我的原型(prototype)的标识符还没有问题,所以有什么问题吗? 这是我的 NewsTableViewController 的代码:
class NewsTableViewController: UITableViewController {
var listOfDisplays = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfDisplays.count
}
func fetchEventsNames()
{
let fetchRequest:NSFetchRequest<Events> = Events.fetchRequest()
do {
let results = try DataBaseControler.getContext().fetch(fetchRequest)
for i in results as [Events]{ /* *dewraper les arguments (i.name)!*/
listOfDisplays.append(i.title!)
}
}
catch
{
print("errors\(error)")
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! NewsTableViewCell
cell.cellImage.image = UIImage(named: "rcbl.jpg")
cell.cellLabel.text = listOfDisplays[indexPath.row]
return(cell)
}
这也是我的 NewsTableViewCells 的代码:
class NewsTableViewCell: UITableViewCell {
@IBOutlet weak var cellLabel: UILabel!
@IBOutlet weak var cellImage: UIImageView!
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
}
然后这是我调用构造函数的代码片段:
...
switch (names[row])
{
case "Actualités":
let destination = NewsTableViewController() // Your destination
destination.listOfDisplays = ["test1", "TEST2"]
navigationController?.pushViewController(destination, animated: true)...
我应该怎么做才能解决这个问题?提前谢谢你们。
最佳答案
当您像这样创建 View Controller 时:
let destination = NewsTableViewController() // Your destination
您只是创建该类的一个实例,而没有在 Storyboard中完成任何设置。
您需要从 Storyboard 中实例化一个,如下所示:
let storyboard = UIStoryboard(name: "Main", bundle: nil) // Use the appropriate story board name
let destination = storyboard.instantiateViewController(withIdentifier: "testtable") // Make sure you have allocated the correct storyboard id to the view controller
self.navigationController?.pushViewController(destination, animated: true)
或者您可以在 viewDidLoad() 中为表格 View 注册单元格类,如下所示:
self.tableView.register(TestTableViewCell.self, forCellReuseIdentifier: "testcell") // Use the correct class and identifier.
关于ios - 无法使用标识符单元 XX 错误使单元出队 - 无法弄清楚如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44440719/