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

我的 UITableViewController 出现了以下错误消息:

由于未捕获的异常而终止应用程序“ NSInternalInconcencyException”,原因: “无法使用标识符取消单元格的队列单元格-必须为标识符注册一个笔尖或一个类,或者在故事板中连接一个原型单元格”

我知道我需要注册一个笔尖或一个类,但我不明白“在哪里或如何?”。

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


}
163950 次浏览

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

或者你有没有把 UITableViewController的类设置成你在那个场景中的类?

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


别忘了 属性督察上要有 识别码

(< em > NOT 为「身分证明文件督察」上的「恢复身分证明文件」 !)

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

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

斯威夫特3 + :

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

使用 Swift 2.2:

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

确保同样的标识符“ cell”也被复制到你的故事板的 UITableViewCell

self”用于获得类,使用类名后跟 .self

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

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

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

enter image description here

别忘了: 在类中声明: UITableViewDataSource

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

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

详情请看这篇文章:

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

这招对我很管用,对你也有帮助:

Swift 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")

我们必须将 识别码属性设置为表格视图单元格,如下图所示,

enter image description here

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

storyboard?.instantiateViewController(withIdentifier: "some_identifier")

我也有同样的问题。这个问题对我很有效。在情节串连板中,选择表格视图并将其从静态单元格更改为动态单元格。

如果你通过 Interface Builder 定义你的细胞,通过在你的 UICollectionView或者 UITableView里面放置一个细胞:

请确保将单元格与您创建的实际类绑定,并且非常重要的一点是,您选中了“ Heritage it module from target”

对于那些新加入 iOS 的朋友(比如我) ,他们决定在不同的 xib 文件中使用多个单元格,解决方案不是使用标识符,而是这样做:

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

这里的 NewsDetails 新闻详情是 xib 文件名。

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

它曾经在 Swift 3和 Swift 4上工作,但现在不工作了。

喜欢

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

因此,我已经尝试了上面提到的大部分解决方案,但没有得到任何运气。

最后,我尝试了这个解决方案,它为我工作。

override func viewDidLoad()
{


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

我的问题是,我正在异步地在分派队列中注册表视图单元。如果您已经在情节串连板中注册了表视图源和委托引用,那么分派队列将延迟单元格的注册,因为名称表明它将异步发生,并且您的表视图正在查找单元格。

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

在“ Subclass of”字段中,选择 UITableViewController。

类标题更改为 xxxxTableViewController。

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

当 IB 中的 UITableView 用 Cmd-C-Cmd-V 移动到另一个子视图时,我遇到了这个消息。

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

唯一的解决方案是清除所有的墨水,相关的表在 IB (出口,数据源,委托)和使他们再次。

匹配两处的标识符名称

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

例如,在我的例子中,标识符是 位置细胞标识符

1)快速文件

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


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

// Your code


return cell
}

2)故事板

enter image description here

确保在用单元格标识符填充的属性中有标识符

我也在为同样的问题而挣扎。实际上,我已经删除了这个类并重建了它。有人,故事板中断了原型细胞和标识符之间的联系。

我删除了标识符名称并重新键入了标识符名称。

成功了。

有两种方法可以定义单元格。如果您的表格单元格位于 ViewControllern 的内部,那么可以这样获取该单元格:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
// write your code here
return cell
}

但是如果您在 ViewController 之外定义 cell,那么可以这样调用 sell:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
// write your code here
return cell
}

就像大家说的,别忘了设置你的手机识别码:

enter image description here

愚蠢的错误:

确保你添加的是 register(TableViewCell.self, forCellReuseIdentifier: "Cell")而不是 register(TableViewCell.self, forHeaderFooterViewReuseIdentifier: "Cell")

如果传统的解决方案(代码或 IB 中类的注册标识符)不起作用: 尝试重新启动 Xcode,结果是我的故事板停止保存我所做的编辑,包括设置重用标识符。

我在 viewDidLoad()中注册自定义 UITableViewCell 类时遇到了同样的问题,这引发了这个错误。为了解决这个问题,我在 didSet属性观察器中注册了单元格,如下所示

@IBOutlet tableview : UITableView! {
didSet {
tableview.register(CustomCell.self, forCellReuseIdentifier: "Cell")
}
}

我的 充满活力表格视图工作正常,在故事板和 dequeueReusableCell(withIdentifier:中设置了单元格标识符。

然后,我将 UITableView 内容从 动态原型切换到 静电细胞

运行应用程序会立即导致错误,尽管单元格的标识符仍然被设置为故事板上的相同值。

对于 静电干扰表视图,必须在情节串连板外注册单元格标识符:

tableView.register(EntryNutritionCell.self, forCellReuseIdentifier: "Cell")

或者,注释掉或者完全删除 cellForRowAtIndexPath:。静态表视图并没有真正使用这个函数,但它仍然被称为(?)并导致了崩溃:

//    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//        return cell
//    }

“表视图单元格”标识符必须与类标识符匹配。

例如: 如果您的“表格视图单元格”标识符名为“ myCellId”,那么您的代码应该是:

let myCell = tableView.dequeueReusableCell(withIdentifier: "myCellId", for: indexPath).

此外,经过几个小时的故障排除后,我意识到在我的 diload ()中有一个 GestureIdentiizer 类不允许我单击表单元格。因此,从 diload ()删除所有“隐藏键盘”功能和其他额外的代码为我解决了这个问题。

如果有人正在 tableView 上进行单元测试,并且您想知道为什么会出现这个错误,那么请确保如果您使用的是文本 fixture,那么您必须在 setUp 函数中正确声明正在测试的系统(SUT) ,否则这个错误将不断出现。调用 loadViewIfNeeded ()也很重要,这样代码和情节串连板之间的出口就连接起来了。

override func setUp() {
super.setUp()
        

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        

sutSearch = storyboard.instantiateViewController(identifier:String(describing: SearchTableViewController.self))
        

sutSearch.loadViewIfNeeded() // To make sure your outlets are connected.
        

}