微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

uitableview – 当我尝试在swift中加载我的tableview时,为什么会出现EXC_BAD_INSTRUCTION错误?

我在将一些值保存到我的coredata数据库后尝试填充我的tableview.我要求它显示我的代码中指示的一些值,但无论我做什么,我都会收到上述错误.
有什么建议?我已尝试过其他一些有这个问题的帖子,但似乎没什么用.

我想不出我可能会给出一个输出Swift没有预料到的地方.

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

    let CellId: Nsstring = "Cell"

    var cell: UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell

    if let ip = indexPath {
        var data: NSManagedobject = myCustomers[ip.row] as NSManagedobject
        var addno = data.valueForKeyPath("custaddno") as String
        var addname = data.valueForKeyPath("custaddfirstline") as String
        cell.textLabel.text = "\(addno) \(addname)"
        var jcost = data.valueForKeyPath("custjobcost") as Float
        var jfq = data.valueForKeyPath("custjobfq") as Float
        var jfqtype = data.valueForKeyPath("custjobfqtype") as String
        cell.detailTextLabel.text = "Charge: \(jcost) to be done every \(jfq) \(jfqtype)"
    }
    return cell
}

解决方法

问题

您可能忘记在之前为单元标识符注册类.请参阅dequeueReusableCellWithIdentifier的文档::

discussion

Prior to dequeueing any cells,call this method or the registerNib:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue,the table view uses the provided @R_616_4045@ion to create a new cell object automatically.

方法返回nil然后Swift无法隐式解包可选.

代码

解决这个问题,你可以在viewDidLoad中调用registerClass:forCellReuseIdentifier :(见docs),就像这样……

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "Cell")
}

在Interface Builder中

…或者,如果您正在使用Storyboard,则在选择UITableViewCell原型时,在属性检查器中直接在Interface Builder中进行设置:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐