簡體   English   中英

無法從可重用的UITableViewCell隊列中加載自定義單元格

[英]Failed to Load Custom Cell from dequeue reusable UITableViewCell

我正在使用xib文件創建自定義單元。 重用標識符在xib文件中設置。 然后我有一個惰性變量,只能用來注冊一次筆尖:

private lazy var registerNib: Bool = {
    let nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
    self.tableView.register(nib, forCellReuseIdentifier: "Custom")

    return true
}()

在單元創建過程中,我只是使用了惰性變量,並使用了xib文件中相同的重用標識符從表格視圖中取出了單元:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let _ = self.registerNib
    let cell = tableView.dequeueReusableCell(withIdentifier: "Custom") as! CustomCell

    return cell
}

但是展開失敗,應用崩潰。

tableView.dequeueReusableCell返回nil

由於某些原因....

有兩種名為dequeueReusableCell方法。

但是展開失敗,應用崩潰。 tableView.dequeueReusableCell 由於某種原因返回nil ....

您正在使用第一個文件,並且文檔中明確指出

返回值

具有關聯identifier UITableViewCell對象;如果在可重用單元格隊列中不存在這樣的對象,則為nil


您可能要使用后者。 更改行:

let cell = tableView.dequeueReusableCell(withIdentifier: "Custom") as! CustomCell

至:

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

您需要在viewDidLoad():注冊您的筆尖對象viewDidLoad():

let nib = UINib(nibName: "CustomTableViewCell", bundle: nil)

tableView.register(nib, forCellReuseIdentifier: "Custom")

另外,您是否在情節提要中設置了單元重用標識符?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM