gpt4 book ai didi

ios - UITableViewCell : dequeued cell is not an instance of cell with identifier

转载 作者:行者123 更新时间:2023-11-28 12:17:44 25 4
gpt4 key购买 nike

我在自定义 UIViewController 中设置了一个 UITableView,我称之为 SavingViewController。我已经将 tableView 的委托(delegate)和数据源设置为 SavingViewController,并且我相信我已经正确设置了 Storyboard中的所有标识符,但是 guard 语句中的 fatal error 总是被调用

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
}

Restoration ID is set

As is the TableViewCell Identifier

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

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")

您正在使用 interface builder 而不是以编程方式进行。删除它,它将起作用。

关于ios - UITableViewCell : dequeued cell is not an instance of cell with identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45893376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com