gpt4 book ai didi

ios - Xcode 找不到我的单元格标识符

转载 作者:可可西里 更新时间:2023-11-01 02:03:06 24 4
gpt4 key购买 nike

enter image description here我的代码出现错误...

"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ItemCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'?"

我已将单元格设置为 ItemCell 并运行了 Product > Clean。它仍然找不到它。谁能看到我做错了什么?我已经包含了我的代码和 Storyboard的屏幕截图。

import UIKit

class ItemsViewController: UITableViewController {

var itemStore: ItemStore!


@IBAction func addNewItem(_ sender: UIButton) {

let newItem = itemStore.createItem()

if let index = itemStore.allItems.index(of: newItem) {
let indexPath = IndexPath(row: index, section: 0)

tableView.insertRows(at: [indexPath], with: .automatic)
}
}

@IBAction func toggleEditingMode(_ sender: UIButton) {
if isEditing {
sender.setTitle("Edit", for: .normal)

setEditing(false, animated: true)
} else {
sender.setTitle("Done", for: .normal)

setEditing(true, animated: true)
}
}


override func viewDidLoad() {
super.viewDidLoad()

let statusBarHeight = UIApplication.shared.statusBarFrame.height

let insets = UIEdgeInsetsMake(statusBarHeight, 0, 0, 0)
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets

tableView.rowHeight = 65
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemStore.allItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell

let item = itemStore.allItems[indexPath.row]


cell.nameLabel.text = item.name
cell.serialNumberLabel.text = item.serialNumber
cell.valueLabel.text = "$\(item.valueInDollars)"

return cell
}

override func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCellEditingStyle,
forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let item = itemStore.allItems[indexPath.row]

let title = "Delete \(item.name)"
let message = "You sure ya wanna delete this?"

let ac = UIAlertController(title: title,
message: message,
preferredStyle: .actionSheet)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
ac.addAction(cancelAction)

let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) -> Void in


self.itemStore.removeItem(item)

self.tableView.deleteRows(at: [indexPath], with: .automatic)
})
ac.addAction(deleteAction)
present(ac, animated: true, completion: nil)
}

}
override func tableView(_ tableView: UITableView,
moveRowAt sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath) {
itemStore.moveItem(from: sourceIndexPath.row, to: destinationIndexPath.row)
}

这是我的 ItemCell

 import UIKit

class ItemCell: UITableViewCell {

@IBOutlet var nameLabel: UILabel!
@IBOutlet var serialNumberLabel: UILabel!
@IBOutlet var valueLabel: UILabel!

}

还有我的 ItemStore

  import UIKit

class ItemStore {

var allItems = [Item] ()


@discardableResult func createItem() -> Item {
let newItem = Item(random: true)

allItems.append(newItem)

return newItem
}


func removeItem(_ item: Item) {
if let index = allItems.index(of: item) {
allItems.remove(at: index)
}
}

func moveItem(from fromIndex: Int, to toIndex: Int) {
if fromIndex == toIndex {
return
}

let movedItem = allItems[fromIndex]

allItems.remove(at: fromIndex)

allItems.insert(movedItem, at: toIndex)
}

}

最佳答案

您必须在 Storyboard 中检查您的自定义类名称作为 ItemCell.swift 作为您的类名称。

Your custom class for tableview cell

Set custom class for tableview

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// create a new cell if needed or reuse an old one
let cell:ItemCell = tableView.dequeueReusableCell(withIdentifier: "ItemCell") as! ItemCell

let item = itemStore.allItems[indexPath.row]


cell.nameLabel.text = item.name
cell.serialNumberLabel.text = item.serialNumber
cell.valueLabel.text = "$\(item.valueInDollars)"

return cell
}

关于ios - Xcode 找不到我的单元格标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986792/

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