【问题标题】:UITableView - "Could not cast value of type 'UITableViewCell' to AppName.CustomCell"UITableView - “无法将 'UITableViewCell' 类型的值转换为 AppName.CustomCell”
【发布时间】:2018-02-01 19:47:19
【问题描述】:

当我尝试打开表格视图时,我在控制台中收到此错误:

“无法将 'UITableViewCell' 类型的值转换为 'AppName.NameTableViewCell'。”

值得注意的几点:

  1. 我没有使用故事板
  2. 我已经在 viewDidLoad() 中将单元格注册到 tableView 中

这里有一些截图来澄清一下。

let tableView = UITableView()
override func viewDidLoad() {
    tableView.register(NameTableViewCell.self, forCellReuseIdentifier: "cellName")
    ...
}

...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cellName") as! NameTableViewCell
        cell.textCell.text = self.menuText[indexPath.row]
        return cell
    }
}

【问题讨论】:

  • 请勿张贴代码图片。 Edit您的问题并将图片替换为您的实际代码,作为文本。
  • 看,你的主要问题在于转换,使用一个或多个单元格并不重要,你应该将单元格出列而不是将常规的 tableview 单元格投射到自定义单元格

标签: ios swift uitableview


【解决方案1】:

您正在尝试将 UITableViewCell 转换为 NameTableViewCell

func tableView(_ tableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if(indexPath.row == 0)
    {
      let cell = tableView.dequeueReusableCell(withIdentifier:"cellName1") as! NameTableViewCell1
      return cell
    }
   else
     if(indexPath.row == 1)
     {
       let cell = tableView.dequeueReusableCell(withIdentifier:"cellName2") as! NameTableViewCell2
       return cell
     }

  }

【讨论】:

    【解决方案2】:

    我已经通过检查目标部分中的单元类解决了这个问题。错过了。

    【讨论】:

      【解决方案3】:

      您需要更改您的代码,以便您正在注册的单元格类实际上在您的cellForRow 方法中使用。试试这个:

      func viewDidLoad() {
          tableView.register(NameTableViewCell.self, forCellReuseIdentifier: "cellName")
          tableView.register(OtherTableViewCell.self, forCellReuseIdentifier: "otherName")
          tableView.register(ThirdTableViewCell.self, forCellReuseIdentifier: "thirdName")
      }
      
      func tableView(_ tableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellName", for: indexPath) as! NameTableViewCell
            // your cell setup code here
            return cell
          } else if indexPath.row == 1 {
             let cell = tableView.dequeueReusableCell(withIdentifier: "otherName", for: indexPath) as! OtherTableViewCell
            return cell
          } else if indexPath.row == 2 {
             let cell = tableView.dequeueReusableCell(withIdentifier: "thirdName", for: indexPath) as! ThirdTableViewCell
            return cell
          }
      }
      

      【讨论】:

      • 对不起,我忘了在我的原始帖子中提及。我将 cellForRowAt: 函数分解为不同的 if 语句的原因是因为我试图为不同的行添加不同的数据类型。所以我的第一行将有一个 textField 然后表的其余部分(在 else 语句中)应该是按钮。所以我不认为我可以像你建议的那样对整个 tableView 使用一个单元格对象。由于不同的单元格将具有不同的对象类型我认为 ^希望我解释清楚
      • 重点是你应该使用tableView.dequeueReusableCell(...)创建单元格,而不是像现在这样显式创建UITableViewCell
      • @dan 你说得对,注册 4000 个单元格也没关系,他会用正确的标识符出列
      • @benjamin852 我已经扩展了我的答案以展示如何处理多个不同的单元格类。只需将它们全部注册在不同的重用标识符下,然后在cellForRow 中选择正确的一个。要点是您必须使用dequeueReusableCell 的两个参数版本才能使其工作。
      • @Gereon 明白了。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多