无法使用标识符Cell将单元格出列 - 必须为标识符注册nib或类,或者在故事板中连接原型单元格

Shi*_*aji 97 iphone ios swift xcode6

我对编码很新,对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)
      }
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

Aks*_*Aks 94

您可以UITableViewCell为此注册一个类:

使用Swift 3+:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)

使用Swift 2.2:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)

确保同样的标识符" cell"也在您的故事板中复制UITableViewCell.

" self"是让类使用后跟的类名.self.

  • 如果在自定义单元格中使用xib,则必须像下面这样注册:`tableView.register(UINib.init(nibName:"CustomCell",bundle:nil),forCellReuseIdentifier:"CustomCellIdentifier")` (18认同)
  • 在`viewDidLoad()`中 (6认同)

小智 72

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

或者您是否已UITableViewController在该场景中为您的班级设置班级?


Jay*_*kar 30

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

Swift 4+:

self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)

Swift 2.2:

self.tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)

我们必须将Identifier属性设置为Table View Cell,如下图所示,

在此输入图像描述

  • 有效!我在身份检查器中设置 ID,但将其粘贴到属性检查器中对我有用 (2认同)

小智 23

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


xhi*_*oda 18

我的情况我通过在Table View Cell的"Identifier"属性中命名它来解决这个问题:

在此输入图像描述

别忘了:在你的Class中声明:UITableViewDataSource

 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
Run Code Online (Sandbox Code Playgroud)


Roh*_*ngh 14

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

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

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

1) Swift 文件

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

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

    return cell
}
Run Code Online (Sandbox Code Playgroud)

###2) 故事板 在此处输入图片说明


LC *_*C 웃 10

只需拖动一个单元格(就像你为TableViewController所做的那样)并通过释放TableViewController上的单元格来添加它.单击单元格并转到其属性检查器并将其标识符设置为"单元格".希望它可以工作.


不要忘记在Attributes Inspector上想要标识符.

(不是 "身份检查员"中的"恢复身份证"!)

  • 注意令人困惑的"恢复身份证" - 这不算什么!单击右上角的第四个选项卡,而不是第三个选项卡! (6认同)

小智 7

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

storyboard?.instantiateViewController(withIdentifier: "some_identifier")
Run Code Online (Sandbox Code Playgroud)


小智 6

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

tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)