无法使用标识符Cell出队 – 必须为该标识符注册一个nib或一个类,或者在故事板中连接一个原型单元格

对于Xcode(Swift)来说,我对编码一般都比较陌生。 我知道我需要注册一个笔尖或一个class级,但我不明白“在哪里或如何?”。

import UIKit class NotesListViewController: UITableViewController { @IBOutlet weak var menuButton: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "preferredContentSizeChanged:", name: UIContentSizeCategoryDidChangeNotification, object: nil) // Side Menu if self.revealViewController() != nil { menuButton.target = self.revealViewController() menuButton.action = "revealToggle:" self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) } } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // whenever this view controller appears, reload the table. This allows it to reflect any changes // made whilst editing notes tableView.reloadData() } func preferredContentSizeChanged(notification: NSNotification) { tableView.reloadData() } // #pragma mark - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return notes.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell let note = notes[indexPath.row] let font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) let textColor = UIColor(red: 0.175, green: 0.458, blue: 0.831, alpha: 1) let attributes = [ NSForegroundColorAttributeName : textColor, NSFontAttributeName : font, NSTextEffectAttributeName : NSTextEffectLetterpressStyle ] let attributedString = NSAttributedString(string: note.title, attributes: attributes) cell.textLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) cell.textLabel?.attributedText = attributedString return cell } let label: UILabel = { let temporaryLabel = UILabel(frame: CGRect(x: 0, y: 0, width: Int.max, height: Int.max)) temporaryLabel.text = "test" return temporaryLabel }() override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) label.sizeToFit() return label.frame.height * 1.7 } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { notes.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } // #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if let editorVC = segue.destinationViewController as? NoteEditorViewController { if "CellSelected" == segue.identifier { if let path = tableView.indexPathForSelectedRow() { editorVC.note = notes[path.row] } } else if "AddNewNote" == segue.identifier { let note = Note(text: " ") editorVC.note = note notes.append(note) } } } } 

你有没有在你的故事板中设置表格单元格的标识符为“单元格”?

或者你有没有把UITableViewController的类设置到你的类中?

使用Swift3.0你需要像这样为你的UITableViewCell注册一个类:

 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 

确保相同的标识符“单元格”也被复制到故事板的UITableViewCell中。

“自我”是让class级使用class级名称,然后是自己

我今天有这个问题,这是通过select产品 – >清洁解决。 因为我的代码是正确的,所以我很困惑。 这个问题从使用command-Z开始太多次了:)

我的情况我解决了这个问题,通过在表格视图单元格的“标识符”属性中命名:

在这里输入图像说明

不要忘记:在你的类中声明:UITableViewDataSource

  let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell 

只需拖动一个单元格(就像您为TableViewController所做的那样),然后通过释放TableViewController上的单元格来添加它。 点击单元格,并转到其属性检查器,并将其标识符设置为“单元格”。希望它能正常工作。


不要忘记你需要属性检查器的 标识符

不是 “身份检查员”上的“恢复ID”)!

在Swift 3.0中,像这样为你的UITableViewCell注册一个类:

 tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell") 

我刚刚遇到同样的问题,看到这个职位。 对我来说,这是因为我忘记了设置单元格的标识符,也正如其他答案中提到的。 我想说的是,如果您使用故事板来加载自定义单元格,我们不需要在代码中registry格视图单元格,这可能会导致其他问题。

看到这个post的细节:

自定义表格视图单元格:IBOutlet标签为零

试试这是为我工作,

 self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell") 

希望这对一些人有帮助。