2

I'm trying to create a TableViewCell, using a XIB file, but I get this error at execution time:

2013-01-10 17:54:50.297 MainApp[6778:b603] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'DropDownCell''
*
Call stack at first throw:

This is what I'm trying to do.
I have a project, with a first Menu. In this project/workspace, I have the class for my first Menu. Inside this project, I have another workspace, with the classes for my second Menu. I mean, this workspace is for the SubViewController's and classes for the options selected in my first Menu. In this workspace, I'm trying to create a DropDownMenu,using the demo from apple, but my app crash. This demo creates the cell's in the table, using a XIB file.
This is the DropDownCell class:
DropDownCell.h

#import <UIKit/UIKit.h>

@interface DropDownCell : UITableViewCell{
  IBOutlet UILabel *textLabel;
  IBOutlet UIImageView *arrow_up;
  IBOutlet UIImageView *arrow_down;

  BOOL isOpen;
}

-(void)setOpen;
-(void)setClosed;

@property (nonatomic)BOOL isOpen;
@property (nonatomic,retain) IBOutlet UILabel *textLabel;
@property (nonatomic,retain) IBOutlet UIImageView *arrow_up;
@property (nonatomic,retain) IBOutlet UIImageView *arrow_down;

@end

DropDownCell.m #import "DropDownCell.h"

@implementation DropDownCell

@synthesize textLabel, arrow_up, arrow_down, isOpen;

-(void)setOpen{
   [arrow_down setHidden:YES];
   [arrow_up setHidden:NO];
   [self setIsOpen:YES];
}

-(void)setClosed{
   [arrow_down setHidden:NO];
   [arrow_up setHidden:YES];
   [self setIsOpen:NO];
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if(self){

   }
   return self;
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
   [super setSelected:selected animated:animated];
}

-(void)dealloc{
   [super dealloc];
}

@end

And the DropDownCell.xib has one UITableViewCell with a UILabel.
I have another UITableViewController, which uses the DropDownCell XIB. This is the method:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"MenuCell";
  static NSString *DropDownCellIdentifier = @"DropDownCell";

  if([indexPath row] == 0){
      DropDownCell *cell = (DropDownCell*)[tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier];

      if(cell == nil){
          NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"DropDownCell" owner:self options:nil];

          for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[DropDownCell class]]){
                cell = (DropDownCell*)currentObject;
                break;
            }
          }
      }

      [[cell textLabel] setText:@"Option 1"];

      return cell;

}

But my app crashes when loads the NIB file... What can be the reason? I'm using Xcode 4.3, and I'm not using storyboards.

El Developer
  • 3,345
  • 1
  • 21
  • 40
user1600801
  • 269
  • 7
  • 25

4 Answers4

1

@user1600801, There may be one of these reasons:-

1) Disable/Uncheck "Use Autolayout" in File Inspector for that Custom cell.

2) Your Target is not set for that Custom Cell.

To set it select your custom cell .Xib file,

Select "File Inspector",

Under "Target membership" check if your project name is selected? If not, Then check/enable it.

3) Check your Custom Cell class name and Cell identifiers.

Javed Iqbal
  • 161
  • 1
  • 9
0

Do you have Auto Layout enabled for that NIB, and are compiling for

Jacek Lampart
  • 1,741
  • 13
  • 25
  • I don't understand whats or how to have the Auto Layout :S... At my XIB file, in the File Inspector -> Target Membership, I have selected my Target... – user1600801 Jan 11 '13 at 01:03
  • On that very same File Inspector tab, in the "Interface Builder Document" section, you have a small checkbox which says "Use Autolayout". If it is checked, your application will crash on iOS versions older than 6.0. – Jacek Lampart Jan 11 '13 at 01:24
  • I don't have that "checkbox". I just have: Document Versioning: Deployment->Project SDK Version (iOS 4.3), Development->Interface Builder 3.0 | Localization Locking: Default->Nothing – user1600801 Jan 11 '13 at 01:50
0

The problem is that you are not even loading the bundle. See this question:

NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle

It seems like you can sort it out by just removing files from your project and put again there.

Community
  • 1
  • 1
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • I get the same error.... Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSBundle loadNibNamed:owner:topLevelObjects:]... Also, I get this warning... "NSBundle may not respond to -loadNibNamed:owner:topLevelObject"... – user1600801 Jan 11 '13 at 01:42
  • I edited it later, in the second part of the answer you see the reason of the warning. – Ramy Al Zuhouri Jan 11 '13 at 01:46
  • I tried to use "inithWithNibName", but also crashes and I also get a warning, because "UITableViewCell" doesn't implement "initWithNibName – user1600801 Jan 11 '13 at 01:56
  • What I've said is to use a UIViewController. Inside the view controller you also load the UITableViewCell. – Ramy Al Zuhouri Jan 11 '13 at 02:03
  • So, I need to create another ViewController, like DropDownViewController. With this DropDownViewController, have a XIB file, with a View, with my UITableViewCell? And load my DropDownViewController in my table, instead of load DropDownCell? – user1600801 Jan 11 '13 at 02:08
  • And load DropDownViewController not in the table, with the initWithNibName method inside the class itself. Even if technically you can do it both ways, but I think the one I suggest is easier. – Ramy Al Zuhouri Jan 11 '13 at 02:14
  • I see that you're missing basics, join the chatroom I've created: http://chat.stackoverflow.com/rooms/22552/uiviewcontroller-load-a-nib-file – Ramy Al Zuhouri Jan 11 '13 at 02:15
  • i can 't join to the chatroom. It says that I don't have 20 in reputation – user1600801 Jan 11 '13 at 23:45
  • I deleted DropDownCell.xib and create again and still have the same error – user1600801 Jan 13 '13 at 03:02
0

Try to remove the references of the nib file and clean the project. Then add the nib file back to the project and build. This resolves the same exception happened in my project.

WeichengChu
  • 116
  • 3