HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了uitableview – 启动应用程序xCode时的EXC_BAD_INSTRUCTION断点大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序中有一个tableview,当我启动应用程序时,它崩溃了以下功能. @H_696_5@ @H_696_5@
func tableView(tableView: UITableView!,cellForRowATindexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell
}
@H_696_5@它在var cell的行上崩溃了

@H_696_5@它给出以下错误

@H_696_5@我无法弄清楚我的代码有什么问题.

@H_696_5@整个功能

@H_696_5@
func tableView(tableView: UITableView!,cellForRowATindexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell

    let data: NsmanagedObject = mylist[ip.row] as NsmanagedObject

    cell.textLabel.text = data.valueForKeyPath("voornaam") as String
    cell.detailTextLabel.text = data.valueForKeyPath("achternaam") as String
    return cell
}
@H_696_5@编辑:
我现在得到的:(仍然给出相同的错误)

@H_696_5@
func tableView(tableView: UITableView!,cellForRowATindexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell? = tableView?.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell

    if cell == nil {
        cell = UITableViewCell(style: .Subtitle,reusEIDentifier: cellId)
    }

    let data: NsmanagedObject = mylist[indexPath.row] as NsmanagedObject

    cell!.textLabel.text = data.valueForKey("voornaam") as String
    cell!.detailTextLabel.text = data.valueForKey("achternaam") as String
    //cell!.textLabel.text = "Hoi"
    return cell
}

解决方法

发生这种情况是因为as运算符被定义为将对象强制转换为给定类型,并在转换失败时崩溃.在这种情况下,第一次调用时,对dequeue的@L_674_12@nil.你需要使用as?运算符,它将尝试将给定对象强制转换为类型,并仅在转换成功时返回具有值的可选项: @H_696_5@ @H_696_5@
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell

if cell == nil {
  cell = UITableViewCell(style: .Subtitle,reusEIDentifier: cellId)
}

...
@H_696_5@因为单元格现在是可选值,请使用单元格!当你想调用它上面的方法来强制解包其中的UITableViewCell.

@H_696_5@此外,您的代码还有第二个问题:它从未创建过新的单元格. dequeue将在第一次在表视图调用时返回nil.您需要在我的代码示例中实例化一个新的UITableViewCell,然后从cellFor …方法返回它.然后,表视图将保存单元格并在将来调用时将其返回到队列.

大佬总结

以上是大佬教程为你收集整理的uitableview – 启动应用程序xCode时的EXC_BAD_INSTRUCTION断点全部内容,希望文章能够帮你解决uitableview – 启动应用程序xCode时的EXC_BAD_INSTRUCTION断点所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。