ios 无法将“UITableViewCell”类型的值转换为“(AppName).(CustomCellName)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29812168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:44:24  来源:igfitidea点击:

Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'

iosxcodeuitableviewswift

提问by obo20

I'm currently trying to create a custom table view cell using xCode 6.3 swift 1.2. For some reason in the cellforRowAtIndexPath method, I just can't seem to set up my cell variable. The code will compile, but then when this line of code hits:

我目前正在尝试使用 xCode 6.3 swift 1.2 创建自定义表格视图单元格。出于某种原因,在 cellforRowAtIndexPath 方法中,我似乎无法设置我的单元格变量。代码将被编译,但是当这行代码命中时:

    var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell

I get this Error: Could not cast value of type 'UITableViewCell' (0x1112f5a18) to 'CampusExchange.MessageCell' (0x10e8318f0).

我收到此错误:无法将“UITableViewCell”(0x1112f5a18)类型的值转换为“CampusExchange.MessageCell”(0x10e8318f0)。

Here's my full method: (I'm using Parse if you're wondering about how I'm setting the message)

这是我的完整方法:(如果您想知道我如何设置消息,我正在使用 Parse)

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell

    let message = messagesArray[indexPath.row]["Message"] as? String
    cell.messageOutlet.text = message
    return cell
}

thanks for any help you might have. I just can't seem to get this to work.

感谢您提供的任何帮助。我似乎无法让它发挥作用。

回答by Rodrigo Pinto

There are a few things you can check in this scenario:

在这种情况下,您可以检查以下几点:

  1. See if your table is linked to your class, usually by @IBOutlet weak var tableView: UITableView!

  2. Register custom table view cell: self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")Note that you use "UITableViewCell" and the identifier "cell" even if your custom cell has different class and id.

  3. Dequeue your cell in cellForRowAtIndexPath: let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCellNow you use the correct cell identifier.

  1. 查看您的表是否与您的班级相关联,通常通过 @IBOutlet weak var tableView: UITableView!

  2. 注册自定义表格视图单元格:self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")请注意,即使您的自定义单元格具有不同的类和 ID,您也使用“UITableViewCell”和标识符“单元格”。

  3. 在 cellForRowAtIndexPath 中出列您的单元格:let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell现在您使用正确的单元格标识符。

回答by Madhurya Gandi

Register your CustomCell in viewDidLoad()method:

viewDidLoad()方法中注册您的 CustomCell :

self.tableView.register(CustomCell.self, forCellReuseIdentifier: "Cell")

回答by Deepti Raghav

I had same problem , I set custom class for cell but forgot to set module enter image description here

我有同样的问题,我为单元格设置了自定义类但忘记设置模块 在此处输入图片说明

after selecting module like this , it worked

选择这样的模块后,它起作用了

enter image description here

在此处输入图片说明

回答by Long Pham

In viewDidLoad()

在 viewDidLoad()

// register custom table view cell from nib
self.tableView.registerNib(UINib(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "messageCell")

回答by Patrik Rikama-Hinnenberg

X Code 9.4
Unlike one of the popular answer here this solution fixed my nightmare with custom tableview cells. If you are using storyboard and everything is hooked up correctly then you do not have to register the the custom tableView cell in viewDidLoad.

Remove from viewDidLoad:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

X Code 9.4
与这里流行的答案之一不同,此解决方案解决了我使用自定义 tableview 单元格的噩梦。如果您正在使用故事板并且一切都正确连接,那么您不必在 viewDidLoad 中注册自定义 tableView 单元格。

从 viewDidLoad 中删除:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

回答by Milander

I've had the same error message. But for me the problem was that I didn't set the class of my custom-cell in the interface builder.

我有同样的错误信息。但对我来说,问题是我没有在界面构建器中设置自定义单元的类。

回答by iOS

In my case i'm not changed the UITableViewCell class. Initially it's DetailsTableViewCell class but i removed and added new class called TableViewCellClass. But I forget to change the cell class name here.

就我而言,我没有更改 UITableViewCell 类。最初它是 DetailsTableViewCell 类,但我删除并添加了名为 TableViewCellClass 的新类。但是我忘记在这里更改单元类名称。

So once again check and change TableViewCell class name heresee below screen shot

所以再次检查并更改 TableViewCell 类名,请参见下面的屏幕截图

enter image description here

在此处输入图片说明

回答by Arafin Russell

Keep the custom cell class name and cell id same:

保持自定义单元类名称和单元 id 相同:

   self.tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")

回答by bj97301

Swift 3 syntax:

Swift 3 语法:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

回答by Hymansonsox

I had the same error. As obo2O states in his comment, his solution worked for me:

我有同样的错误。正如 obo2O 在他的评论中所说,他的解决方案对我有用:

  1. Go to the Interface Builder
  2. Click on the relevant storyboard
  3. Click on the relevant cell within the tableview
  4. In the Utilities Panel (right side), click on the "Show the Identify Inspector" icon
  5. In the "Custom Class" section -> "Class" field, set the class to anything else and run the application. The application crashes, for classX cannot cast to classY
  6. In the "Custom Class" section -> "Class" field, set the class to the correct class and re-run.
  1. 转到界面生成器
  2. 点击相关故事板
  3. 单击 tableview 中的相关单元格
  4. 在实用程序面板(右侧)中,单击“显示识别检查器”图标
  5. 在“自定义类”部分 ->“类”字段中,将类设置为其他任何内容并运行应用程序。应用程序崩溃,因为 classX 无法转换为 classY
  6. 在“自定义类”部分->“类”字段中,将类设置为正确的类并重新运行。