2017-07-08 20 views
2

enter image description here我得到我的代碼一個錯誤,指出...Xcode中無法找到我的小區標識

「終止應用程序由於未捕獲的異常 ‘NSInternalInconsistencyException’,理由是:「無法出列具有標識符ItemCell的單元格 - 必須爲 標識符註冊一個筆尖或類別,或者在故事板「?」中連接原型單元格

我已經將單元格設置爲ItemCell並且我運行了Product> Clean。它仍然無法找到它。任何人都可以看到我做錯了什麼?我已經包含了我的代碼和故事板的屏幕截圖。

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

} 
+0

什麼是原型細胞中的故事板類的類型? – iAviator

+0

1.您在哪裏註冊tableViewCell? 2.在你的代碼中搜索「註冊」...你有什麼發現?共享所有實例。 – Honey

+0

只需嘗試刪除表視圖層次結構下的TableViewCell上方存在的UIView,或者是否將UITableViewController的類設置爲該場景中的類? –

回答

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

檢查一下您是否設置單元格的正確的自定義類(ItemCell在這種情況下)的身份檢查。