這可能是一些非常愚蠢的,因爲我很新的雨燕和iOS開發,但我有一個UITableView
,並子類UITableViewCell
(稱之爲myCell
)。斯威夫特的UITableViewCell子類,IBOutlet中的UILabel錯誤,如果正常使用VAR沒有錯誤
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!
,並在初始化器中像上面那樣實例化它,事情就起作用了。
如果它的事項,我有一個UIViewController
的UITableView
(而不是一個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在故事板的電池原型。
是的,我已經覆蓋了這兩個。 – andrewm921
您是否從故事板場景創建視圖控制器實例? – Paulw11
您的零售店將在您的初始化程序中爲零。在設置其值之前,您必須等待視圖加載。 –