繁体   English   中英

TableView单元格错误-iOS / Swift

[英]TableView Cell Error - iOS/Swift

运行tableView cellForRowAtIndexPath时出现此错误:

致命错误:解开Optional值时意外发现nil

我不明白,我已经正确分配了标识符ID,但是错误地分配了..

这是我的代码:

当覆盖有趣的cellForRowAtIndexPath时:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("CellDataflow") as UITableViewCell

        cell.textLabel.text = "This is a title."
        return cell
}

这是我称之为视图的地方:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   ….more code…
    self.navigationController?.pushViewController(MainTVC(), animated: true)

}

注意=>我将这种方式称为新视图,因为我有一个自定义单元格,因此无法从情节提要中调用它。

谢谢

编辑:该行中的错误:

让单元格= tableView.dequeueReusableCellWithIdentifier(“ CellDataflow”)作为UITableViewCell

我不是Swift专家,但我查看了文档,该方法返回了一个可选值:

func dequeueReusableCellWithIdentifier(_ identifier: String) -> AnyObject?

也许它返回nil ,尝试使用as? 而不是as并检查nil

您还不需要索引路径吗?

所以:

let cell = tableView.dequeueReusableCellWithIdentifier("IDENTIFIER", forIndexPath: indexPath) as UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as UITableViewCell

我认为这将帮助您...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM