我在我的自定義單元格文件在tableview中有問題。我設法使用下面顯示的註釋行來完成它,但是當它有10多個單元時,性能非常糟糕。 UsingdequeueReusableCell導致這個錯誤:TableView CellReuse
「NSInternalInconsistencyException」,原因:「無法出列具有標識符DiveNewsShort細胞 - 必須註冊標識符的筆尖或一類或在一個情節串連圖板連接原型細胞」
這很奇怪,因爲我在viewDidLoad()中註冊了nib。我希望你能幫助我,我對此感到沮喪。
class ProfilTableView: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "DiveNewsShort", bundle: nil), forCellReuseIdentifier: "DiveNewsShort")
tableView.register(DiveNewsShort.self, forCellReuseIdentifier: "DiveNewsShort")
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = Bundle.main.loadNibNamed("DiveNewsShort", owner: self, options: nil)?.first as! DiveNewsShort
// This one works as expected
let cell = tableView.dequeueReusableCell(withIdentifier: "DiveNewsShort", for: indexPath) as! DiveNewsShort
// This one does not
return cell }
更新:
我設法通過在cellForRowAt功能將寄存器功能擺脫錯誤的,但我不認爲這是一個有效的實際上。它應該在vieDidLoad內工作嗎?
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UINib(nibName: "DiveNewsShort", bundle: nil), forCellReuseIdentifier: "DiveNewsShort")
let cell = tableView.dequeueReusableCell(withIdentifier: "DiveNewsShort", for: indexPath) as! DiveNewsShort
return cell }
你爲什麼要爲'tableView.register'對同一個標識符進行兩次調用? – rmaddy
在NIB中設置基類,不註冊類,只註冊NIB。或者在故事板中使用單元格原型,並且根本不需要NIB,也不需要手動註冊任何內容。但是如果你在多個表中重用NIB,那麼註冊NIB是好方法(但顯然沒有任何單元原型)。 – Rob
您是否在故事板中設置了單元格標識符? – mat