Xcode找不到我的小区标识符

时间:2017-07-08 13:31:33

标签: ios swift xcode

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

  

“因未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'无法将单元格出列   标识符ItemCell - 必须注册一个nib或类   标识符或连接故事板中的原型单元'?'

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

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

 }

2 个答案:

答案 0 :(得分:1)

您必须在故事板中将自定义班级名称视为 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
    }

答案 1 :(得分:0)

检查您是否在Identity Inspector中为单元格设置了正确的自定义类(在本例中为ItemCell)。