TableViewCell:取消排队的单元格不是具有标识符的单元格的实例



我在自定义UIViewController中设置了一个UITableView,我称之为SavingViewController。我已将表视图的委托和数据源设置为 SavingViewController,并且我相信我已经正确设置了情节提要中的所有标识符,但始终调用 guard 语句中的致命错误

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "WishlistTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? WishlistTableViewCell else {
fatalError("The dequeued cell is not an instance of WishlistTableViewCell.")
}
return cell
}

已设置恢复 ID

与 TableViewCell 标识符一样

------完整代码-------

import UIKit
class SavingViewController: UIViewController {
var totalSavings: Double = 0
var items: [WishListItem] = []
@IBOutlet weak var savingsView: UIView!
@IBOutlet weak var wishlistView: UIView!
@IBOutlet weak var scroll: UIScrollView!
@IBOutlet weak var wishlistTitle: UILabel!
@IBOutlet weak var savingsLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var toolbar: UIToolbar!

override func viewDidLoad() {
super.viewDidLoad()
items = DummyPurchaseData.returnDummyItemsArray()
tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "WishlistTableViewCell")
tableView.reloadData()
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addWishButton(_:)))
let editButton = editButtonItem
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil);
toolbar.items = [editButton, flexibleSpace, addButton]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func addWishButton(_ sender: UIBarButtonItem){
//addWish()
print("Do nothing")
}
func addItemToWishlist(item: WishListItem) {
items.append(item)
}
}
extension SavingViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "WishlistTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? WishlistTableViewCell else {
fatalError("The dequeued cell is not an instance of WishlistTableViewCell.")
}
return cell
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
items.remove(at: indexPath.row)
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
}
}
}

不需要这一行-:

tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "WishlistTableViewCell")

您正在使用接口构建器,而不是以编程方式执行此操作。删除它,它将工作.

Istead of

tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "WishlistTableViewCell")

您应该具备:

tableView.register(WishlistTableViewCell.self,
forCellReuseIdentifier: "WishlistTableViewCell")

最新更新