cast value of type 'UITableViewCell' to Custom Cell

為{幸葍}努か 提交于 2019-11-26 22:09:07

问题


I usually never have a problem with this step, but for some reason when I re set up my storyboard now I have an issue with this part of my code:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

    (cell as! TableViewCell).usernameLabel.text = friendsArray[indexPath.row] as String


    return cell
}

I have done all of the obvious stuff like making sure the cell identifier is "Cell".

My error is this:

Could not cast value of type 'UITableViewCell' (0x1094ada18) to 'YOpub.TableViewCell' (0x106620410).

EDIT: Problem was I had self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") in my didLoad function. After removing this line my code worked again.


回答1:


  • You may have forgotten to tell the storyboard that this cell is a TableViewCell. Select the prototype cell and set the Custom Class in the Identity inspector.

  • You may have called registerClass:forCellReuseIdentifier:. If so, delete that call; it actually prevents us from getting the cell from the storyboard.

  • You are calling tableView.dequeueReusableCellWithIdentifier. That is a big mistake. You should call tableView.dequeueReusableCellWithIdentifier:forIndexPath:.




回答2:


One of the reason,May be in Identity inspector module is different then your project name .



来源:https://stackoverflow.com/questions/30429269/cast-value-of-type-uitableviewcell-to-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!