我的 UITableViewController 导致崩溃并显示以下错误消息:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有标识符 Cell 的单元格出列 - 必须为标识符注册 nib 或类或连接情节提要中的原型单元格”
我知道我需要注册笔尖或课程,但我不明白“在哪里或如何?”。
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)
}
}
}
}
您可以像这样为您的 UITableViewCell
注册一个课程:
使用 Swift 3+:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
使用 Swift 2.2:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
确保在情节提要的 UITableViewCell
中也复制了相同的标识符“cell
”。
“self
”用于让类使用类名后跟 .self
。
您是否在情节提要中将表格单元格标识符设置为“单元格”?
或者您是否已将 UITableViewController
的类设置为该场景中的类?
这对我有用,也可以帮助你:
斯威夫特 4+:
self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
斯威夫特 3:
self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
斯威夫特 2.2:
self.tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
我们必须按照下图将 Identifier 属性设置为 Table View Cell,
https://i.stack.imgur.com/rjx3f.png
我今天遇到了这个问题,通过选择 Product -> Clean 解决了这个问题。我很困惑,因为我的代码是正确的。问题从使用 command-Z 太多次开始:)
我的情况是我通过在表格视图单元格的“标识符”属性中命名它来解决这个问题:
https://i.stack.imgur.com/39Ryb.png
不要忘记:在你的类中声明:UITableViewDataSource
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
只需拖动一个单元格(就像您对 TableViewController 所做的那样)并通过释放 TableViewController 上的单元格将其添加到其中。单击单元格,然后转到其属性检查器并将其标识符设置为“单元格”。希望它可以工作。
不要忘记你想要属性检查器上的标识符。
(不是“身份检查员”上的“恢复 ID”!)
匹配两个地方的标识符名称
当 Swift 文件和 Storyboard 中的 Tablecell 的标识符名称不同时,会发生此错误。
例如,在我的例子中,标识符是 placecellIdentifier。
1) 斯威夫特文件
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "placecellIdentifier", for: indexPath)
// Your code
return cell
}
2) 故事板
https://i.stack.imgur.com/jv0oK.png
发生此问题的另一个原因是较早的问题。当显示一个新的 ViewController 时,直接实例化目标 ViewController 当然不会从 StoryBoard 加载原型单元格。正确的解决方案应该始终是通过故事板实例化视图控制器,如下所示:
storyboard?.instantiateViewController(withIdentifier: "some_identifier")
在 Swift 3.0 中,为您的 UITableViewCell 注册一个类,如下所示:
tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell")
我有同样的问题。这个问题对我有用。在情节提要中选择您的表格视图并将其从静态单元格更改为动态单元格。
我的问题是我在调度队列中异步注册表视图单元格。如果您在情节提要中注册了表格视图源和委托引用,那么调度队列将延迟单元格的注册,顾名思义它将异步发生,并且您的表格视图正在寻找单元格。
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()
}
愚蠢的错误:
确保添加 register(TableViewCell.self, forCellReuseIdentifier: "Cell")
而不是 register(TableViewCell.self, forHeaderFooterViewReuseIdentifier: "Cell")
如果您通过 Interface Builder 定义了您的单元格,则通过在您的 UICollectionView
或 UITableView
中放置一个单元格:
确保您将单元格与您创建的实际类绑定,非常重要的是,您检查了“从目标继承模块”
它曾经在 swift 3 和 swift 4 上工作,但现在它不工作了。
喜欢
self.tableView.register(MyTestTableViewCell.self, forCellReuseIdentifier: "cell")
所以我在swift 5中尝试了上面提到的大部分解决方案,但没有得到任何运气。
最后我尝试了这个解决方案,它对我有用。
override func viewDidLoad()
{
tableView.register(UINib.init(nibName: "MyTestTableViewCell", bundle: nil), forCellReuseIdentifier: "myTestTableViewCell")
}
有两种方法可以定义单元格。如果您的表格单元格位于 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 之外定义单元格,则以这种方式调用 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
}
正如大家所说,不要忘记设置您的单元格标识符:
https://i.stack.imgur.com/T0JbN.png
我刚遇到同样的问题,看到这个帖子。对我来说,这是因为我忘记了设置单元格的标识符,也如其他答案中所述。我想说的是,如果您使用情节提要加载自定义单元格,我们不需要在代码中注册表格视图单元格,这可能会导致其他问题。
有关详细信息,请参阅此帖子:
Custom table view cell: IBOutlet label is nil
斯威夫特 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")
}
我在 viewDidLoad()
中注册了我的自定义 UITableViewCell 类时遇到了同样的问题,这引发了这个错误。为了解决这个问题,我在 didSet
属性观察器中注册了单元格,如下所示
@IBOutlet tableview : UITableView! {
didSet {
tableview.register(CustomCell.self, forCellReuseIdentifier: "Cell")
}
}
对于那些决定在不同的 xib 文件中拥有多个单元格的 iOS 新朋友(比如我),解决方案不是拥有标识符,而是这样做:
let cell = Bundle.main.loadNibNamed("newsDetails", owner: self, options: nil)?.first as! newsDetailsTableViewCell
这里的 newsDetails 是 xib 文件名。
当 IB 中的 UITableView 使用 Cmd-C - Cmd-V 移动到另一个子视图时,我遇到了此消息。
IB 中的所有标识符、委托方法、链接等都保持不变,但在运行时会引发异常。
唯一的解决方案是清除所有与 IB 中的 tableview 相关的墨迹(出口、数据源、委托)并再次制作它们。
如果有人在 tableView 上进行单元测试并且您想知道为什么会出现此错误,只需确保如果您使用的是文本夹具,则必须在 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.
}
在“子类”字段中,选择 UITableViewController。
类标题更改为 xxxxTableViewController。保持原样。
确保选中“同时创建 XIB 文件”选项。
确保属性中的标识符填写了您的单元格标识符
我也在努力解决同样的问题。我实际上已经删除了课程并重建了它。有人,故事板已经删除了原型单元和标识符之间的链接。
我删除了标识符名称并再次重新键入了标识符名称。
有效。
如果经典解决方案(在代码或 IB 中注册类标识符)不起作用:尝试重新启动 Xcode,结果我的故事板停止保存我所做的编辑,包括设置重用标识符。
我的 dynamic 表格视图工作正常,在情节提要和 dequeueReusableCell(withIdentifier:
中设置了单元格标识符。
然后我将 UITableView 内容从动态原型切换到静态单元格。
运行应用程序会立即导致错误,尽管单元格的标识符仍设置为故事板上的相同值。
对于静态表格视图,您必须在 Storyboard 之外注册单元格标识符:
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).
此外,经过数小时的故障排除后,我意识到在我的 didLoad() 中有一个 GestureRecognizer 类不允许我单击表格单元格。因此从 didLoad() 和其他额外代码中删除所有“隐藏键盘”功能为我解决了这个问题。
不定期副业成功案例分享
viewDidLoad()
tableView.register(UINib.init(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCellIdentifier")