[英]Why is my UILabel nil?
我有一个从表格单元格中的三个UILabel到自定义类的链接,该类是与VC(视图控制器)分开的文件。 listOfTasks
是一本里面有元组的字典。 以下是相关代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as! MyCustomTableViewCell
let (name,description,date) = listOfTasks[indexPath.row]
cell.title.text = name //Here I get the error: "fatal error: unexpectedly found nil while unwrapping an Optional value"
cell.subtitle.text = description
cell.date.text = date
return cell
}
为什么给我这个错误?
我已经检查过我的连接了。
PS对于想这人是重复此 ,在回答这个问题不解决我的问题。
编辑: MyCustomTableViewCell
代码:
import UIKit
class MyCustomTableViewCell: UITableViewCell {
@IBOutlet weak var title: UILabel!
@IBOutlet weak var subtitle: UILabel!
@IBOutlet weak var date: UILabel!
}
变量:
cell.title
: nil
cell.subtitle
: nil
cell.date
: nil
编辑2: FirstViewController
屏幕截图:
编辑3:
也许我没有彻底检查我的连接。
班上空的圈子是正常的吗?
参见编辑7。
编辑4:无论何时何地,只要尝试为它分配.text
值,我都会收到相同的错误。 我尝试在与之链接的课程中这样做; 同样的错误。
编辑5:这似乎不是我的联系薄弱的事实; 强仍然给我错误。
编辑6:我将我的项目文件上传到GitHub , 以供有兴趣查看它们的任何人使用。
编辑7:设法使@IBOutlet
正常工作。
我认为您应该删除以下代码(如果存在):
self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell")
单击XCode右上角的两个交错的圆圈。 这会将屏幕分为两部分。 确保一侧是故事板,另一侧是UITableViewCell的自定义子类。 接下来,单击+按住并从每个空白圆圈拖动到正确的视图(例如,对于标签,请确保已将其连接到所需的标签。)
在执行此操作之前,您可能还希望清除单元中的所有现有连接。 在情节提要中执行此操作,按住shift键并右键单击tableView中的单元格。 确保从下拉列表中选择单元格。 然后,单击屏幕右上方带有圆圈的箭头。 这将向您显示现有的插座连接。 只需按现有的旁边的x,就不会有任何“僵尸商店”。
当我最后一个自定义TableViewCell类时,我不得不添加如下代码:
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
您是否正在实现UITableViewCell的所有必需方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.