将“UITableViewCell"类型的值转换为自定义单元格 [英] cast value of type 'UITableViewCell' to Custom Cell

查看:27
本文介绍了将“UITableViewCell"类型的值转换为自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常不会在这一步遇到问题,但出于某种原因,当我现在重新设置故事板时,我的这部分代码有问题:

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
}

我已经完成了所有显而易见的事情,例如确保单元标识符为Cell".

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

我的错误是这样的:

无法将 'UITableViewCell' (0x1094ada18) 类型的值转换为 'YOpub.TableViewCell' (0x106620410).

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

问题是我的 didLoad 函数中有 self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") .删除这一行后,我的代码再次运行.

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

推荐答案

  • 您可能忘记告诉故事板这个单元格一个 TableViewCell.选择原型单元格并在身份检查器中设置自定义类.

    • 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.

      您可能已经调用了 registerClass:forCellReuseIdentifier:.如果是,请删除该调用;它实际上阻止我们从故事板中获取单元格.

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

      您正在调用 tableView.dequeueReusableCellWithIdentifier.这是一个很大的错误.您应该调用 tableView.dequeueReusableCellWithIdentifier:forIndexPath:.

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

      这篇关于将“UITableViewCell"类型的值转换为自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆