2014-10-19 111 views
0

當前正在使用Xcode版本6.0.1並嘗試創建自己的自定義tableViewCell。 我已經設置了XIB文件出現像這樣Xib File With Code使用故事板在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)

+0

如果在調用崩潰'dequeueReusableCellWithIdentifier'它可能是你沒有正確設置你的小區標識。由於檢查員在屏幕上不可見,我不能說是否是這種情況。同時檢查您是否已將所有插座連接到正確的目標。儘管他們應該有點點滴滴,但你可能已經將兩個網點連接到同一個目標。 – 2014-10-19 11:18:19

回答

0

你的cellForRowAtIndexPath:應該是這樣的,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 

    var cell:RecipesTableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell2") as? RecipesTableViewCell 

    if cell == nil{ 
     let nib = UINib(nibName: "RecipesTableViewCell", bundle: nil) 
     let allViews: [AnyObject]? = nib?.instantiateWithOwner(self, options: nil) 
     cell = allViews?.last as? cell:RecipesTableViewCell 
    } 

    cell?.prepTimeLabel.text = "5 mins" 
    cell?.nameLabel.text = "HellO" 
    return cell! 
} 

但是,最好使用register nib,然後使用tableView.dequeueReusableCellWithIdentifier:forIndexPath:method。

+0

我的xib文件名叫做RecipeTableCell.xib(只是說它可能有助於回答Q) 所以我複製你的版本的方法,我得到這個錯誤 ***終止應用程序由於未捕獲異常'NSInternalInconsistencyException',原因:'無法在我的.xib文件中加載NIB捆綁:' 我通過cellResuseIdenfier命名爲cell2,並在故事板文件中刪除了原型tableViewCell,以便它只是一個空表View。 – stringRay2014 2014-10-19 12:39:01

1

你在你的UITableViewController類註冊筆尖:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.registerNib(UINib(nibName: "RecipesTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "cell2") 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell:RecipesTableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell2") as? RecipesTableViewCell 

    cell?.prepTimeLabel.text = "5 mins" 
    cell?.nameLabel.text = "HellO" 

    return cell! 
} 
+0

當我跑我們的代碼時得到這個錯誤 ***由於未捕獲的異常'NSInternalInconsistencyException',原因:'無法加載包中的NIB: 與上述答案同樣錯誤! 僅供參考我的.xib文件被稱爲RecipeTableCell.xib,而我的自定義TableViewCell類被稱爲RecipesTableViewCell – stringRay2014 2014-10-19 12:44:53

+0

您是否已驗證您的nib文件是否位於複製資源構建步驟?我在Xcode 6上遇到了一些問題,無法正確複製筆尖,因此值得檢查該筆包並檢查筆尖是否存在。 – 2014-10-19 12:59:59

+0

@LevLandau如何驗證筆尖文件? – stringRay2014 2014-10-19 19:46:10