【问题标题】:two tableviews with two different tableview cells具有两个不同 tableview 单元格的两个 tableview
【发布时间】:2018-07-18 09:50:49
【问题描述】:

我有两个带有两个不同自定义表格视图单元格的表格视图。我正在这样做:

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
    }
}

当然,我已经从情节提要中注册了它们......

但我不断得到这个:

在这一行:

let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as!过滤表视图单元格

尝试像这样注册它们:

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

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

但仍然出现同样的错误!

做错了什么?!

【问题讨论】:

  • 控制台中的完整错误信息是什么?你错过了一步。但这可能是因为您没有注册类,您没有注册 xib(如果它在自定义 xib 中),您没有在单元格属性中设置正确的类/标识符等。
  • @Larme 我做到了!并仔细检查! :(
  • 向我们展示您注册 xib 的行
  • @Nitish 刚刚更新了我的问题
  • 检查我的答案,你只是输入错误。

标签: ios swift xcode


【解决方案1】:

问题是您在 else 部分输入了“tableview”而不是“tableView”:

替换:

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

与:

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

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

【讨论】:

  • 天啊!!!是的,这就是问题所在!我不敢相信!! :|哈哈,无论如何,非常感谢!!!!
  • 大声笑,这是让我永远不会创建对带有 tableview 名称的 UITableView 的引用的原因之一:D
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    可能的原因:

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

    【讨论】:

    • 请在您的帖子中添加单元格注册。然后在返回它之前尝试运行并调试这两种类型的单元格中的哪一个是 nil
    • 刚刚做了 .. 而 nil 是 filtercell
    • 好的,所以,试着了解,如果从表 filterdrop 表中删除委托,另一个是否正常工作。让我们看看问题只是那个表还是我们缺少一些东西
    • 是的,从过滤表视图中删除了代表..另一个表视图工作正常,没有错误
    • 您为单元格视图创建了 xib,还是在其表格中创建了单元格?你能提供xib或storyboard vc吗?
    【解决方案4】:

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

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

    //

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

    或者你将出队行换成了表格

    【讨论】:

    • 我在 cellForRowAt 里面做这个?还是在 viewdidload 中?
    • 我试过这个..但仍然没有工作..得到同样的错误:(
    • 在viewDidLoad中
    【解决方案5】:

    在 Storyboard 中查看您的 Tableview 控制器。您应该有另一个 indentier,而不是调用:

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

    【讨论】:

      【解决方案6】:

      顺便说一句,您应该始终使用这个较新的功能

      dequeueReusableCell(withIdentifier:for:)
      

      而不是

      dequeueReusableCell(withIdentifier:)
      

      详情请见this post

      【讨论】:

      • 嗯,如果我认为 tableviewcell 的大小不同,则需要调用新函数
      • @MarkWarriors:不,不是真的。单元格大小是动态完成的(UITableViewAutomaticDimension)或通过委托方法tableView(_:heightForRowAt:)。这与您如何使单元格出列无关。
      • 是的,我知道,但是如果您的所有单元格都具有相同的高度和宽度,那么“新功能”就毫无用处,因为对于应用程序在该索引处搜索单元格的大小是无用的。 . 因为和其他人一样大
      【解决方案7】:

      您可以轻松解决这个问题,只需检查您当时使用的 tableview。 你可以试试这个代码,

      func tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) {

      if (tableview == (yourtableview 1)) {

      让 cell = yourtableview 1.dequeueReusableCell(withIdentifier: "yourtableview Cell 1") as? yourtableview 单元格 1

      // 为 tableview 1 编写代码 返回单元格 }

      否则 { 让 cell = yourtableview 2.dequeueReusableCell(withIdentifier: "yourtableview Cell 2") as? yourtableview 单元格 2

      // 为 tableview 2 编写代码 返回单元格 }

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        • 2013-01-23
        • 1970-01-01
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        相关资源
        最近更新 更多