【发布时间】:2021-03-11 11:35:05
【问题描述】:
我在一个视图控制器中使用了两个表格视图,并且我已经为情节提要中的表格视图单元格提供了两个标识符
对于这两个单元格,我在情节提要中添加了标识符,我错了,请帮助我
我为下面的两个表格视图编写了代码
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()
}
}
错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 ServiceDetailsTableViewCell1 的单元格出列 - 必须为标识符注册一个 nib 或类或连接故事板中的原型单元格” 以 NSException 类型的未捕获异常终止
【问题讨论】: