简体   繁体   English

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

[英]Swift - Fatal error: unexpectedly found nil while unwrapping an Optional values

I am relatively new to coding with Swift and working with the Xcode IDE. 我对使用Swift进行编码和使用Xcode IDE相对较新。 I am trying to implement a new Table View Controller into the storyboard. 我正在尝试在情节提要中实现新的Table View Controller。 I have done everything I know how to do this, but I get the 我已经做了所有我知道该怎么做的事情,但是我得到了

Fatal error: unexpectedly found nil while unwrapping an Optional values 致命错误:解开Optional值时意外发现nil

when I click through to the screen in the simulator. 当我单击进入模拟器中的屏幕时。

import UIKit

class DrivingTipsTableViewController: UITableViewController
{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int
    {
        return 5
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell =  tableView.dequeueReusableCellWithIdentifier("DrivingLesson") as UITableViewCell
        let label = cell.viewWithTag(69) as UILabel

        if indexPath.row == 0
        {
            label.text = "Setup & ball position"
        }
        else if indexPath.row == 1
        {
            label.text = "Driving lesson 2"
        }
        else if indexPath.row == 2
        {
            label.text = "Driving lesson 3"
        }
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

}

It shows the breakdown at the let label = line. 它在let label =行显示故障。 Looking through some answers on this site, it may be the dequeueReusableCellWithIdentifier("DrivingLesson") that needs changing, but it corresponds with the identifier I gave my cell in the IDE, so I really have no idea where I am going wrong. 通过查看此站点上的一些答案,可能是需要更改的dequeueReusableCellWithIdentifier("DrivingLesson") ,但它与我在IDE中为单元格赋予的标识符相对应,因此我真的不知道我要去哪里。

First, you should use this method in your first line of your cell for index path: 首先,您应该在单元格的第一行中使用此方法作为索引路径:

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

Then delete let label = cell.viewWithTag(69) as UILabel and just set UITableViewCell's textLabel property: 然后删除let label = cell.viewWithTag(69) as UILabel并设置UITableViewCell的textLabel属性:

cell.textLabel.text = "Some text"

暂无
暂无

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

相关问题 Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value 快速致命错误:解开Optional值时意外发现nil - swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在Swift 3中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3 SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value Swift中的可选类型错误:致命错误:解开可选值时意外发现nil - Optional type error in Swift: fatal error: unexpectedly found nil while unwrapping an Optional value Xcode Swift:appDelgate.window! is nil :致命错误:在解开可选值时意外发现 nil - Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value 迅速-播放声音时出现错误“严重错误:在展开可选值时意外发现nil” - Swift - While playing sound I get error “fatal error: unexpectedly found nil while unwrapping an Optional value” 致命错误:在Tableview中展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Tableview 致命错误:解开可选值(lldb)时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM