这可能是非常愚蠢的,因为我对Swift和iOS开发还很陌生,但是我有一个UITableView
并把UITableViewCell
子类化(称为myCell
)。myCell
在情节提要中有一个UILabel
,我将其连接到代码@IBOutlet weak var myLabel: UILabel!
上,以使其旁边的小点变为灰色。然后在初始化器override init(style: UITableViewCellStyle, reuseIdentifier: String?)
中,访问myLabel
时出现以下错误-不管是否首先实例化它(myLabel = UILabel()
):fatal error: unexpectedly found nil while unwrapping an Optional value
但是,如果我将myLabel
声明为标准var
,var myLabel: UILabel!
,并在初始化程序中如上所述将其实例化,则一切正常。
如果很重要,我将UITableView
放在UIViewController
中(而不是UITableViewController
),因此视图控制器将实现UITableViewDataSource
和UITableViewDelegate
,并将表视图添加为子视图。
我的问题是,在通过界面生成器工作时,为什么myLabel
是nil
?
(我已经检查了连接,并删除了连接并创建了一个新标签-通过ctrl +从情节提要UILabel
拖到代码中,然后通过从代码中的圆圈中拖动来简单地重新连接到情节提要UILabel
。)
编辑
通过以下方式调用我的表格单元格:let cell = tableView.dequeueReusableCellWithIdentifier("MyTableViewCell", forIndexPath: indexPath) as! MyTableViewCell
给出以下错误:'unable to dequeue a cell with identifier MyTableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
...我不明白,因为“ MyTableViewCell”被设置为情节提要中单元原型的reuseIdentifier。
最佳答案
情节提要中的UILabel通过特定的redirectIdentifier连接到特定的单元格。您是否要在与其所在的单元格不同的单元格中访问标签?这可能会导致您遇到错误。
关于ios - Swift UITableViewCell子类,IBOutlet UILabel错误,如果用作普通变量则没有错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36851457/