ios 无法使用标识符 Cell 使单元出列 - 必须为标识符注册一个笔尖或类,或者连接故事板中的原型单元

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29282447/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:21:35  来源:igfitidea点击:

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

iosswiftiphonexcode6

提问by ShizukaNaHaji

I am fairly new to coding in general and really new to Xcode (Swift). I understand that I need to register a nib or a class but I don't understand 'where or how?'.

我对一般编码相当陌生,对 Xcode (Swift) 真的很陌生。我知道我需要注册笔尖或课程,但我不明白“在哪里或如何?”。

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)
      }
    }
  }

}

采纳答案by Race B

Have you set the Table Cellidentifier to "Cell" in your storyboard?

您是否在故事板中将表格单元格标识符设置为“单元格”?

Or have you set the class for the UITableViewControllerto your class in that scene?

或者你是否UITableViewController在那个场景中为你的班级设置了班级?

回答by Aks

You can register a class for your UITableViewCelllike this:

你可以UITableViewCell像这样注册一个类:

With Swift 3+:

使用 Swift 3+:

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

With Swift 2.2:

使用 Swift 2.2:

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

Make sure same identifier "cell" is also copied at your storyboard's UITableViewCell.

确保cell在故事板的UITableViewCell.

"self" is for getting the class use the class name followed by .self.

self”是为了让类使用类名后跟.self

回答by Jaywant Khedkar

This worked for me, May help you too :

这对我有用,也可以帮助你:

Swift 4+ :

斯威夫特 4+:

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

Swift 3:

斯威夫特 3

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

Swift 2.2:

斯威夫特 2.2

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

We have to Set Identifierproperty to Table View Cell as per below image,

我们必须按照下图将Identifier属性设置为 Table View Cell,

enter image description here

在此处输入图片说明

回答by MacInnis

I had this issue today which was solved by selecting Product -> Clean. I was so confused since my code was proper. The problem started from using command-Z too many times :)

我今天遇到了这个问题,通过选择 Product -> Clean 解决了这个问题。我很困惑,因为我的代码是正确的。问题开始于多次使用 command-Z :)

回答by xhinoda

y my case i solved this by named it in the "Identifier" property of Table View Cell:

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

enter image description here

在此处输入图片说明

Don't forgot: to declare in your Class: UITableViewDataSource

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

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

回答by Anish Parajuli ?

Just drag a cell (as you did for TableViewController) and add in to it just by releasing the cell on TableViewController. Click on the cell and.Go to its attributes inspector and set its identifier as "Cell".Hope it works.

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



Don't forget you want Identifieron the Attributes Inspector.

不要忘记您需要Attributes Inspector上的Identifier

(NOTthe "Restoration ID" on the "Identity Inspector" !)

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

回答by Peter Honeder

One more reason for this issue to happen is an earlier problem. When showing a new ViewController, instantiating the target ViewController directly will of course not load the prototype cells from the StoryBoard. The correct solution should always be to instantiate the view controller through the story board like this:

发生此问题的另一个原因是较早的问题。当显示一个新的 ViewController 时,直接实例化目标 ViewController 当然不会从 StoryBoard 加载原型单元。正确的解决方案应该始终是通过故事板实例化视图控制器,如下所示:

storyboard?.instantiateViewController(withIdentifier: "some_identifier")

回答by ak_ninan

In Swift 3.0, register a class for your UITableViewCell like this :

在 Swift 3.0 中,为您的 UITableViewCell 注册一个类,如下所示:

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

回答by Rohit Singh

Match the identifier name at both places

匹配两个地方的标识符名称

This error occurs when the identifier name of the Tablecell is different in the Swift file and The Storyboard.

当 Swift 文件和故事板中 Tablecell 的标识符名称不同时,会发生此错误。

For example, the identifier is placecellIdentifierin my case.

例如,在我的例子中,标识符是placecellIdentifier

1) The Swift File

1) Swift 文件

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

    // Your code 

    return cell
}

2) The Storyboard

2)故事板

enter image description here

在此处输入图片说明

回答by Scaraux

If you defined your cell through the Interface Builder, by placing a cell inside your UICollectionView, or UITableView:

如果您通过 Interface Builder 定义了您的单元格,通过在您的UICollectionView, 或 中放置一个单元格UITableView

Make sure you binded the cell with an actual class you created, and very important, that you checked "Inherit module from target"

确保您将单元格与您创建的实际类绑定,非常重要的是,您选中了“从目标继承模块”