加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

uitableview – 当我尝试在swift中加载我的tableview时,为什么会

发布时间:2020-12-14 04:57:20 所属栏目:百科 来源:网络整理
导读:我在将一些值保存到我的coredata数据库后尝试填充我的tableview.我要求它显示我的代码中指示的一些值,但无论我做什么,我都会收到上述错误. 有什么建议?我已尝试过其他一些有这个问题的帖子,但似乎没什么用. 我想不出我可能会给出一个零输出Swift没有预料到
我在将一些值保存到我的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 information 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中进行设置:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读