[英]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.