无法使 swift 中具有标识符 ServiceDetailsTableViewCell1 的单元格出队
unable to dequeue a cell with identifier ServiceDetailsTableViewCell1 in swift
我将两个表格视图合二为一viewcontroller,并且我已经在故事板中为表格视图单元格提供了两个标识符
对于两个单元格,我都在情节提要中添加了标识符,我错了请帮助我
我为下面的两个表视图编写了代码
class ServiceDetailsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var detailsTableviewheight: NSLayoutConstraint!
@IBOutlet weak var detailsTableView: UITableView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var heightOfTableView: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
detailsTableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let tableview = object as? UITableView
if tableview == self.tableView{
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
let newsize = newvalue as! CGSize
heightOfTableView.constant = newsize.height
}
}
}
if tableview == self.detailsTableView{
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
let newsize = newvalue as! CGSize
detailsTableviewheight.constant = newsize.height
}
}
}
}
deinit {
if tableView.observationInfo != nil { tableView.removeObserver(self, forKeyPath: "contentSize")
}
if detailsTableView.observationInfo != nil { tableView.removeObserver(self, forKeyPath: "contentSize")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableView{
return 10
}
if tableView == detailsTableView{
return 5
}
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == detailsTableView{
let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceDetailsTableViewCell1", for: indexPath) as! ServiceDetailsTableViewCell1
return cell
}
if tableView == tableView{
let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceDetailsTableViewCell", for: indexPath) as! ServiceDetailsTableViewCell
return cell
}
return UITableViewCell()
}
}
错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ServiceDetailsTableViewCell1 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
terminating with uncaught exception of type NSException
您首先需要注册您的自定义 UITableviewCell
,然后再将其出列:
tableView.register(ServiceDetailsTableViewCell1.self, forCellReuseIdentifier: "ServiceDetailsTableViewCell1")
tableView.register(ServiceDetailsTableViewCell.self, forCellReuseIdentifier: "ServiceDetailsTableViewCell")
我将两个表格视图合二为一viewcontroller,并且我已经在故事板中为表格视图单元格提供了两个标识符
对于两个单元格,我都在情节提要中添加了标识符,我错了请帮助我
我为下面的两个表视图编写了代码
class ServiceDetailsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var detailsTableviewheight: NSLayoutConstraint!
@IBOutlet weak var detailsTableView: UITableView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var heightOfTableView: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
detailsTableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let tableview = object as? UITableView
if tableview == self.tableView{
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
let newsize = newvalue as! CGSize
heightOfTableView.constant = newsize.height
}
}
}
if tableview == self.detailsTableView{
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
let newsize = newvalue as! CGSize
detailsTableviewheight.constant = newsize.height
}
}
}
}
deinit {
if tableView.observationInfo != nil { tableView.removeObserver(self, forKeyPath: "contentSize")
}
if detailsTableView.observationInfo != nil { tableView.removeObserver(self, forKeyPath: "contentSize")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableView{
return 10
}
if tableView == detailsTableView{
return 5
}
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == detailsTableView{
let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceDetailsTableViewCell1", for: indexPath) as! ServiceDetailsTableViewCell1
return cell
}
if tableView == tableView{
let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceDetailsTableViewCell", for: indexPath) as! ServiceDetailsTableViewCell
return cell
}
return UITableViewCell()
}
}
错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ServiceDetailsTableViewCell1 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' terminating with uncaught exception of type NSException
您首先需要注册您的自定义 UITableviewCell
,然后再将其出列:
tableView.register(ServiceDetailsTableViewCell1.self, forCellReuseIdentifier: "ServiceDetailsTableViewCell1")
tableView.register(ServiceDetailsTableViewCell.self, forCellReuseIdentifier: "ServiceDetailsTableViewCell")