Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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! MessageCell Now you use the correct cell identifier.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...