我有两个表视图和两个不同的自定义tableview单元..我这样做:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableview {
let cell = tableview.dequeueReusableCell(withIdentifier: "matchescell") as! MatchesTableViewCell
// ......
return cell
} else {
let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell
// ......
return cell
}
}
当然我已经从故事板上注册了它们......
但我一直这样:
在这条线上:
让cell = tableview.dequeueReusableCell(withIdentifier:“filtercell”)为! FilterTableViewCell
试图像这样注册他们:
tableview.register(MatchesTableViewCell.self, forCellReuseIdentifier: "matchescell")
filterdrop.register(FilterTableViewCell.self, forCellReuseIdentifier: "filtercell")
但仍然得到同样的错误!
做错了什么?!
问题是你在其他部分输入了“tableview”而不是“tableView”:
替换:
let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell
用:
let cell = tableView.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell
它崩溃是因为你的“tableview”没有注册这个单元格。
在storyboard或xib自定义单元格(FilterTableViewCell)中添加“filtercell”标识符。
可能的原因:
这是因为您要么没有注册单元格
tableView.register(CustomCellClass.self, forCellReuseIdentifier: "CellID")
//
tableView.register(UINib(nibName: "CustomCellClass", bundle: nil), forCellReuseIdentifier: "CellID")
或者你换了表格的出队线
在Storyboard中查看Tableview控制器。你应该有另一个不同于下的契约:
let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell
顺便说一下,你应该总是使用这个新功能
dequeueReusableCell(withIdentifier:for:)
代替
dequeueReusableCell(withIdentifier:)
有关详细信息,请参阅this post。
您可以轻松解决此问题,只是为了检查您当时使用的tableview。你可以尝试这段代码,
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath){
if(tableview ==(yourtableview 1))
{
让cell = yourtableview 1.dequeueReusableCell(withIdentifier:“yourtableview Cell 1”)为? yourtableview Cell 1}
else {let cell = yourtableview 2.dequeueReusableCell(withIdentifier:“yourtableview Cell 2”)为? yourtableview Cell 2
}
返回细胞
}