0

Im currently using Xcode version 6.0.1 and trying to create my own custom tableViewCell. I've set up the xib file to appear like so Xib File With Code

Code for RecipeTableViewCell Class

import UIKit

class 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
}

}

In my tableViewController class my code looks like this for the main method that controls the cell.

 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!
}

Any time i run the app it gives me

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

1
  • If it is crashing at the call to dequeueReusableCellWithIdentifier it might be that you have not set your cell identifier correctly. As the inspector is not visible in your screen I cannot say whether this is the case. Also check that you have connected all the outlets to the correct target. That have dots as they should, but you might have connected two outlets to the same target. Commented Oct 19, 2014 at 11:18

2 Answers 2

1

You have to register the nib in your UITableViewController class:

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!
}
Sign up to request clarification or add additional context in comments.

5 Comments

Got This error when i ran ur code *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: Same error as the above answer as well!. FYI my .xib file is called RecipeTableCell.xib and my custom TableViewCell class is called RecipesTableViewCell
Have you verified that your nib file is in the copy resources build step? I have had some problems with Xcode 6 not copying nibs correctly so it is worth examining the bundle and check that the nib is there.
@LevLandau How Can I Verify the nib file?
What I did was to locate to open Finder, select GO from the menu and "Go to Folder". Enter ~/Library and and navigate to Developer/Xcode. Inside there you should be able to find to find the application bundle for a debug or release build. Searching for all files of type nib inside the bundle I noticed some where missing. I was a able to work around the issue by moving the xib files away from the en.lproj folder.
I had the same problem and the registerNib function worked for me. Tks @zisoft
0

Your cellForRowAtIndexPath: should look like this,

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!
}

However, it is better to use register nib and then use tableView.dequeueReusableCellWithIdentifier:forIndexPath: method.

1 Comment

my xib file name is called RecipeTableCell.xib (just incase it might help in answering the Q) So i copied your version of the method and i got this error *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: ' in my .xib file i named by cellResuseIdenfier as cell2 and in the storyboard file i removed the protoype tableViewCell so that its just an empty table View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.