Ios 无法将标识符为cell的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格
一般来说,我对编码相当陌生,对Xcode(Swift)也非常陌生。我知道我需要注册一个nib或一个类,但我不知道“在哪里或如何注册?”Ios 无法将标识符为cell的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格,ios,swift,iphone,xcode6,Ios,Swift,Iphone,Xcode6,一般来说,我对编码相当陌生,对Xcode(Swift)也非常陌生。我知道我需要注册一个nib或一个类,但我不知道“在哪里或如何注册?” import UIKit class NotesListViewController: UITableViewController { @IBOutlet weak var menuButton: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad()
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)
}
}
}
}
在故事板中是否已将表格单元格标识符设置为“单元格”
或者您是否已将UITableViewController
的类设置为该场景中的类?只需拖动一个单元格(就像您对TableViewController所做的那样),然后通过释放TableViewController上的单元格将其添加到该单元格中。单击单元格,然后转到其属性检查器,将其标识符设置为“单元格”。希望它能工作
不要忘记您想要在属性检查器上设置标识符
(不是身份检查器上的“恢复ID”!)我今天遇到了这个问题,通过选择Product->Clean解决了这个问题。我很困惑,因为我的代码是正确的。问题源于多次使用command-Z:)您可以像这样为您的
UITableViewCell
注册一个类:
storyboard?.instantiateViewController(withIdentifier: "some_identifier")
使用Swift 3+:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
使用Swift 2.2:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
确保同样的标识符“cell
”也被复制到故事板的UITableViewCell
“
self
”用于获取类。在Swift 3.0中,使用类名后跟。self
,为UITableViewCell注册一个类,如下所示:
tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell")
在我的案例中,我通过在表视图单元格的“Identifier”属性中命名解决了这个问题: 不要忘记:在类中声明:UITableViewDataSource
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
我刚刚遇到了同样的问题,看到了这篇文章。对我来说,这是因为我忘记了设置单元格的标识符,这也是其他答案中提到的。我想说的是,如果您使用故事板加载自定义单元格,我们不需要在代码中注册表视图单元格,这可能会导致其他问题 有关详细信息,请参阅此帖子:
这对我很有用,也可能对你有所帮助: Swift 4+:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
Swift 3:
self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
self.tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
Swift 2.2:
self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
self.tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
如下图所示,我们必须为表视图单元格设置标识符属性
发生此问题的另一个原因是早期的问题。当显示新的ViewController时,直接实例化目标ViewController当然不会从故事板加载原型单元。正确的解决方案应该始终是通过故事板实例化视图控制器,如下所示:
storyboard?.instantiateViewController(withIdentifier: "some_identifier")
我也有同样的问题。这个问题对我很有效。在序列图像板中选择表格视图,并将其从静态单元格更改为动态单元格。如果通过界面生成器定义了单元格,请将单元格放置在
UICollectionView
或UITableView
中:
确保将单元格与您创建的实际类绑定在一起,并且非常重要的是,您选中了“从目标继承模块”对于那些决定拥有多个单元格并位于不同xib文件中的iOS新手(如我),解决方案不是要有标识符,而是要这样做:
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上工作,但现在不工作了 像 因此,我在swift 5中尝试了上述大多数解决方案,但没有得到任何运气 最后我尝试了这个解决方案,它对我有效
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()
}
在“的子类”字段中,选择UITableViewController 类标题更改为XXTableViewController。就这样吧
确保选择了“还创建XIB文件”选项。当IB中的UITableView被移动到另一个带有Cmd-C-Cmd-V的子视图时,我遇到了这条消息 IB中的所有标识符、委托方法、链接等保持不变,但在运行时引发异常 唯一的解决方案是清除IB中与tableview相关的所有墨迹(outlet、datasource、delegate),然后重新制作 在两个位置匹配标识符名称 当表格单元的标识符名称在Swift文件和情节提要中不同时,会发生此错误 例如,在我的例子中,标识符是placecellIdentifier 1) Swift文件 ###2) 故事板
确保在属性中用单元格标识符填写标识符我也遇到了同样的问题。我实际上已经删除了这个类并重新构建了它。有人说,故事板删除了原型单元和标识符之间的链接 我删除了标识符名称并再次键入标识符名称
它成功了。有两种方法可以定义单元格。如果表格单元格位于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
}
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
}
但是,如果在ViewController之外定义单元格,则按以下方式调用sell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
// write your code here
return cell
}
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
}
正如大家所说的,不要忘记设置您的手机标识符:
愚蠢的错误:
确保添加寄存器(TableViewCell.self,forCellReuseIdentifier:“Cell”)
而不是寄存器(TableViewCell.self,forHeaderFooterViewReuseIdentifier:“Cell”)
如果经典解决方案(在代码或IB中为类注册标识符)不起作用:尝试重新启动Xcode,结果我的故事板停止了保存编辑内容