无法使带有标识符Cell的单元出队-必须为该标识符注册一个笔尖或一个类或在情节提要中连接原型单元


113

对于一般的编码我还是相当陌生,对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)
      }
    }
  }

}

11
当心令人困惑的“恢复ID”-这没什么! 单击右上角的“ 第四”选项卡,而不是“第三”选项卡!!
Fattie

Answers:


81

您是否在情节提要中将表格单元格标识符设置为“单元格”?

还是您将的课程设置UITableViewController为该场景中的课程?


102

您可以UITableViewCell像这样注册一个班级:

使用Swift 3+:

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

使用Swift 2.2:

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

确保在标识符cell上也复制了相同的标识符“ ” UITableViewCell

self”用于获取类,请使用类名后面加上.self


1
你把它放在哪里?
RJB

7
viewDidLoad()
Mette

18
如果您在自定义单元格中使用了xib,则必须像这样注册:tableView.register(UINib.init(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCellIdentifier")
atulkhatri

以上迅速3+解决方案,迅速5为我工作
麦克Volmar

38

这对我有用,也可能对您有帮助:

迅捷4+:

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

迅捷3

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

Swift 2.2

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

我们必须根据下图将“ 标识符”属性设置为“表格视图单元格”,

在此处输入图片说明


1
就这么简单:-)
卢克·奥利维尔

1
有效!我在身份检查器中设置了ID,但将其粘贴到属性检查器中对我却有用
Fato

1
上帝祝福你!救了我一生
Ragen Dazs

26

我今天遇到了这个问题,方法是选择“产品”->“清洁”。因为我的代码正确,所以我很困惑。问题是从使用command-Z太多次开始的:)


1
认真花费了一个多小时来调试它。谢谢您:)
ewakened

当然,它与在情节
提要中

21

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

在此处输入图片说明

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

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

该图片不匹配,但给了一些提示。
Martian2049

这是一个有点过时的只是
Martian2049

13

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


别忘了要在Attributes Inspector上使用Identifier

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


6
当心令人困惑的“恢复ID”-这没什么!单击右上角的“第四”选项卡,而不是“第三”选项卡!!!
Fattie

9

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

storyboard?.instantiateViewController(withIdentifier: "some_identifier")

是!!这是真的!!这发生在我身上,是因为我过渡到位于不同Storyboard中的ViewController,只是使用了:self.present(MyViewController,animation:false,complete:nil)。看起来它确实没有下载原型!我尝试直接从MyViewController开始,它正常工作!当我在目标ViewController的viewDidLoad中为单元格注册NIB时,它也可以工作。
Vitya Shurapov

7

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

当Swift文件和情节提要中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
}

2)故事板

在此处输入图片说明


5

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

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


2

如果您是通过“界面生成器”定义的单元格,请在UICollectionView或中放置一个单元格,或者UITableView

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


2

它曾经在swift 3和swift 4上工作,但现在不起作用。

喜欢

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

因此,我在快速5中尝试了上面提到的大多数解决方案,但是没有遇到任何麻烦。

最终,我尝试了这种解决方案,并且对我有用。

override func viewDidLoad() 
{

    tableView.register(UINib.init(nibName: "MyTestTableViewCell", bundle: nil), forCellReuseIdentifier: "myTestTableViewCell")
}

2

我的问题是我正在异步地在调度队列中注册表视图单元。如果您已在情节提要中注册了表视图源和委托引用,则分派队列将延迟单元的注册,因为名称暗示它将异步发生,并且表视图正在查找单元。

DispatchQueue.main.async {
    self.tableView.register(CampaignTableViewCell.self, forCellReuseIdentifier: CampaignTableViewCell.identifier())
    self.tableView.reloadData()
}

您要么不应该使用调度队列进行注册,要么这样做:

DispatchQueue.main.async {
    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.tableView.register(CampaignTableViewCell.self, forCellReuseIdentifier: CampaignTableViewCell.identifier())
    self.tableView.reloadData()
}

1

我刚刚遇到了同样的问题,并看到了这篇文章。对我来说,是因为我忘记了设置单元格的标识符,这在其他答案中也提到过。我想说的是,如果您使用情节提要来加载自定义单元格,则无需在代码中注册表格视图单元格,这可能会导致其他问题。

详细信息请参见这篇文章:

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


0

对于那些刚决定拥有多个单元并位于不同xib文件中的iOS伙伴(如我)的新手,解决方案是不具有标识符,但要执行以下操作:

let cell = Bundle.main.loadNibNamed("newsDetails", owner: self, options: nil)?.first as! newsDetailsTableViewCell

这里newsDetails是xib文件名。


0

迅捷5

您需要使用UINib方法在viewDidLoad中注册单元格

override func viewDidLoad() 
{
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //register table view cell        
    tableView.register(UINib.init(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}

0

在“子类”字段中,选择UITableViewController。

类标题将更改为xxxxTableViewController。保持原样。

确保选择了“也创建XIB文件”选项。


0

我将IB中的UITableView移到带有Cmd-C-Cmd-V的另一个子视图中时遇到了此消息。

IB中的所有标识符,委托方法,链接等均保持不变,但在运行时会引发异常。

唯一的解决方案是清除与IB中的表格视图相关的所有墨水(插座,数据源,委托),然后再次进行制作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.