类型' UITableViewCell'的转换值到自定义单元格

时间:2015-05-24 23:04:11

标签: ios uitableview swift tableviewcell

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

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"。

我的错误是:

  

无法转换类型' UITableViewCell' (0x1094ada18)到' YOpub.TableViewCell' (0x106620410)。

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

2 个答案:

答案 0 :(得分:12)

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

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

  • 您正在呼叫tableView.dequeueReusableCellWithIdentifier。这是一个很大的错误。你应该致电tableView.dequeueReusableCellWithIdentifier:forIndexPath:

答案 1 :(得分:1)

其中一个原因,可能是在Identity Insctor模块中与您的项目名称不同。 enter image description here