2017-02-21 25 views
0

我已經創建了兩個的UITableViewCell對象(FriendsCell,AddFriendsCell),並有以下代碼:無法出列具有標識符錯誤細胞儘管正確地識別的小區

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

    if indexPath.row == self.selectedFriends.count { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "AddFriendsCell", for: indexPath) as! AddFriendsCell 
     return cell 

    } else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "FriendsCell", for: indexPath) as! FriendsCell 
    ... 

AddFriendsCell具有按鈕,點擊時,應該原因請看到另一個視圖控制器。然而,當點擊它返回這個有據可查的錯誤:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'unable to dequeue a cell with 
identifier AddFriendsCell - must register a nib or a class for the 
identifier or connect a prototype cell in a storyboard' 
*** First throw call stack: 

我不明白爲什麼存在此錯誤時,相應的原型細胞很明確認定爲「AddFriendsCell」在它的故事板的設置,是類類型AddFriendsCell的(一個UITableViewCell類)

當我繼續與第一dequeueReusableCell行:

tableview.register(AddFriendsCell, forCellReuseIdentifier: "AddFriendsCell") 

結果是在運行時,它代替先前正確格式化AddTableCell的產生空白小區。

請幫我理解爲什麼在按下AddFriendsCell中的這個按鈕時拋出這個錯誤,以及如何糾正它。

+1

你說你正確設置類的原型細胞在你的故事板。在你的Storyboard中,你是否也設置了它的標識符? – Larme

+0

事實上,這個類是AddFriendsCell,標識符是「AddFriendsCell」 – Legatro

回答

1

您需要先註冊單元格才能使用它。例如:

override func viewDidLoad() { 
    super.viewDidLoad() 

    var nib = UINib(nibName: "AddFriendsCell", bundle: nil) 
    tableView.register(nib, forCellReuseIdentifier: "AddFriendsCell") 

} 

UPDATE:使用,如果你沒有xib文件

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.register(AddFriendsCell.self, forCellReuseIdentifier: "AddFriendsCell") 

} 
+0

乾杯。不幸的是,確切的代碼導致了我以前遇到的一個錯誤:未捕獲的異常:'無法加載捆綁NIB:'....(長路徑)....''名稱'AddFriendsCell'' – Legatro

+0

試試吧,它是不是你說的同樣的錯誤。你通過課程,而不是像我這樣的實例。 – mhergon

+0

輸入你的代碼和測試後,它會給出同樣的錯誤。 – Legatro