我正在尝试执行以下操作,但是当我将内容添加到表格时,它会崩溃。有这个错误:
' NSInternalInconsistencyException',原因:'无法使用标识符UtilityTableViewCell对单元格进行出列 - 必须为标识符注册一个nib或类,或者在故事板中连接原型单元格'
import UIKit
class UtilityBillTableViewController: UITableViewController {
var bills = [UtilityBill]()
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return bills.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "UtilityTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! UtilityTableViewCell
let bill = bills[indexPath.row]
cell.billName.text = bill.billName
cell.totalDue.text = bill.amountDue
cell.totalDue.text = bill.dueDate
return cell
}
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
@IBAction func unwindToBillList(sender: UIStoryboardSegue){
if let sourceViewController = sender.source as? UtilitiesViewController, let bill = sourceViewController.bill {
// Add a new meal item.
let newIndexPath = NSIndexPath(row: bills.count, section: 0)
bills.append(bill)
tableView.insertRows(at: [newIndexPath as IndexPath], with: .bottom)
}
}
}
答案 0 :(得分:1)
如果您已在故事板中定义了单元格,则必须在该故事板场景中的原型单元格中指定重用标识符;如果你正在使用NIB或自定义类,你必须注册它。
您应该在故事板的原型单元格中仔细检查单元格标识符的拼写。或者,如果使用NIB或以编程方式创建的单元格,请确保调用适当的寄存器方法。