可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell
I get this Error: Could not cast value of type 'UITableViewCell' (0x1112f5a18) to 'CampusExchange.MessageCell' (0x10e8318f0).
Here's my full method: (I'm using Parse if you're wondering about how I'm setting the message)
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.
回答1:
There are a few things you can check in this scenario:
See if your table is linked to your class, usually by @IBOutlet weak var tableView: UITableView!
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.
Dequeue your cell in cellForRowAtIndexPath: let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell
Now you use the correct cell identifier.
回答2:
Register your CustomCell in viewDidLoad()
method:
self.tableView.register(CustomCell.self, forCellReuseIdentifier: "Cell")
回答3:
In viewDidLoad()
// register custom table view cell from nib self.tableView.registerNib(UINib(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "messageCell")
回答4:
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.
回答5:
I had same problem , I set custom class for cell but forgot to set module 
after selecting module like this , it worked

回答6:
Firstly I suggest you to recreate your cell class, using CocoaClass. I had quite similar mistake - I created a CollectionViewCell, and when I recognised my mistake, I decided to simply rename the parent class. However, compiler haven't notice any mistake, I had this error during build.
回答7:
Swift 3 syntax:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
回答8:
I had the same error. As obo2O states in his comment, his solution worked for me:
- Go to the Interface Builder
- Click on the relevant storyboard
- Click on the relevant cell within the tableview
- In the Utilities Panel (right side), click on the "Show the Identify Inspector" icon
- 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
- In the "Custom Class" section -> "Class" field, set the class to the correct class and re-run.
回答9:
Recreate your class using cocoaClass
and select UItableViewCell
I had the same problem this worked for me.