将'UITableViewCell'类型的值转换为Custom Cell

Jus*_*tin 2 uitableview tableviewcell ios swift

我通常从来没有遇到过这个步骤的问题,但由于某种原因,当我重新设置我的故事板时,我的代码部分存在问题:

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
}
Run Code Online (Sandbox Code Playgroud)

我做了所有明显的事情,比如确保单元格标识符是"Cell".

我的错误是这样的:

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

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

mat*_*att 12

  • 您可能忘记告诉故事板这个单元格 TableViewCell.选择原型单元格并在Identity检查器中设置Custom Class.

  • 你可能打过电话registerClass:forCellReuseIdentifier:.如果是,请删除该呼叫; 它实际上阻止我们从故事板中获取单元格.

  • 你在打电话tableView.dequeueReusableCellWithIdentifier.这是一个很大的错误.你应该打电话tableView.dequeueReusableCellWithIdentifier:forIndexPath:.