當前正在使用Xcode版本6.0.1並嘗試創建自己的自定義tableViewCell。 我已經設置了XIB文件出現像這樣使用故事板在swift中創建自定義TableViewCell
代碼RecipeTableViewCell類
import UIKit
類RecipesTableViewCell:{UITableViewCell的
@IBOutlet weak var recipeImage: UIImageView!
@IBOutlet weak var nameLabel:UILabel!
@IBOutlet weak var prepTimeLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
在我tableViewController類我的代碼看起來像這個控制單元格的主要方法。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:RecipesTableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell2") as? RecipesTableViewCell
if cell == nil{
//var nib:NSArray = NSBundle.mainBundle().loadNibNamed("RecipesTableViewCell", owner: self, options: nil)
var tempController:UIViewController = UIViewController(nibName: "RecipesTableViewCell", bundle: nil)
cell = tempController.view as? RecipesTableViewCell
println(cell)
}
cell?.prepTimeLabel.text = "5 mins"
cell?.nameLabel.text = "HellO"
return cell!
}
任何時候,我運行它給我的應用程序
致命錯誤:意外發現零而展開的可選值 (LLDB)
如果在調用崩潰'dequeueReusableCellWithIdentifier'它可能是你沒有正確設置你的小區標識。由於檢查員在屏幕上不可見,我不能說是否是這種情況。同時檢查您是否已將所有插座連接到正確的目標。儘管他們應該有點點滴滴,但你可能已經將兩個網點連接到同一個目標。 – 2014-10-19 11:18:19