两个带有两个不同tableview单元格的tableviews

问题描述 投票:0回答:7

我有两个表视图和两个不同的自定义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
    }
}

当然我已经从故事板上注册了它们......

enter image description here

enter image description here

enter image description here

enter image description here

但我一直这样:

enter image description here

在这条线上:

让cell = tableview.dequeueReusableCell(withIdentifier:“filtercell”)为! FilterTableViewCell

试图像这样注册他们:

   tableview.register(MatchesTableViewCell.self, forCellReuseIdentifier: "matchescell")

   filterdrop.register(FilterTableViewCell.self, forCellReuseIdentifier: "filtercell")

但仍然得到同样的错误!

做错了什么?!

ios swift xcode
7个回答
2
投票

问题是你在其他部分输入了“tableview”而不是“tableView”:

替换:

let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell

用:

let cell = tableView.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell

它崩溃是因为你的“tableview”没有注册这个单元格。


0
投票

在storyboard或xib自定义单元格(FilterTableViewCell)中添加“filtercell”标识符。


0
投票

可能的原因:

  • 您尚未将单元格的类或笔尖注册到正确的tableview
  • 您没有在nib / storyboard中的cellview中设置标识符
  • 您已反转了tableview及其单元格类型

0
投票

这是因为您要么没有注册单元格

tableView.register(CustomCellClass.self, forCellReuseIdentifier: "CellID")

//

 tableView.register(UINib(nibName: "CustomCellClass", bundle: nil), forCellReuseIdentifier: "CellID")

或者你换了表格的出队线


0
投票

在Storyboard中查看Tableview控制器。你应该有另一个不同于下的契约:

let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell

0
投票

顺便说一下,你应该总是使用这个新功能

dequeueReusableCell(withIdentifier:for:)

代替

dequeueReusableCell(withIdentifier:)

有关详细信息,请参阅this post


-1
投票

您可以轻松解决此问题,只是为了检查您当时使用的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

}

返回细胞

}

© www.soinside.com 2019 - 2024. All rights reserved.