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

标签 ios swift xcode

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/

相关文章:

ios - iPhone模拟器无法打开(localhost)http://127.0.0.1:8000/foo/bar/

ios - 据报告 View 的尺寸已切换

iphone - IOS AddressBook 没有从联系人列表中获取所有电话号码

ios - 在 DeckView 示例中将右 View 移到中心 View 上

ios - View Controller 关闭并重新出现以前的 View Controller

ios - 如何将核心数据关系设置为 NSNull()?

swift - 没有这样的模块 '####'

objective-c - ios核心数据如何实现sql事务功能?

ios - 为什么我以编程方式添加的 UIView 没有连接到我的 UIViewController?

ios - 如何使用 XCode 6.4 下载和替换 AppGroup 容器