1

I need help understanding why my console returns the error:

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle <--APP_LOCATION--> (loaded)' with name 'MyListingsTableViewCell'' ** First throw call stack: ...

I'm new to using these prototype cells and tables, I will attach the code below. I appreciate any support that you can offer.

Code for the view controller where the prototype cell is implemented as follow:

#import "MyListings.h"

@interface MyListings ()

@property (weak, nonatomic) IBOutlet UIButton *createListingButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MyListings

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MyListingsTableViewCell"];

    UINib *nib1 = [UINib nibWithNibName:@"MyListingsTableViewCell" bundle:nil];
    [self.tableView registerNib:nib1 forCellReuseIdentifier:@"MyListingsTableViewCell"];

    titles = [[NSArray alloc]initWithObjects:@"1",@"2", nil];
    currentBids = [[NSArray alloc]initWithObjects:@"3",@"4", nil];
    stillAvailables = [[NSArray alloc]initWithObjects:@"5",@"6", nil];

    _createListingButton.layer.cornerRadius = 8;
    _createListingButton.layer.borderWidth = 1.5f;
    _createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
    [_createListingButton addTarget:self action:@selector(createListingButtonHighlightBorder) forControlEvents:UIControlEventTouchDown];
    [_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchUpInside];
    [_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchDragExit];
}

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

- (void)createListingButtonHighlightBorder
{
    _createListingButton.layer.borderColor = [UIColor colorWithRed:0.61 green:0.00 blue:0.02 alpha:1.0].CGColor;
}

- (void)createListingButtonUnhighlightBorder
{
    _createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [titles count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyListingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingsTableViewCell" forIndexPath:indexPath];
    [cell updateCellWithTitle:[titles objectAtIndex:indexPath.row] currentBid:[currentBids objectAtIndex:indexPath.row] stillAvailable:[stillAvailables objectAtIndex:indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

Prototype Cell class:

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)updateCellWithTitle:(NSString *)title currentBid:(NSString *)bid stillAvailable:(NSString *)available
{
    self.titleLabel.text = title;
    self.currentBidLabel.text = bid;
    self.stillAvailableLabel.text = available;
    self.editLabel.text = @"Press to edit";
}

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

    // Configure the view for the selected state
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • Possible duplicate of [Unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard?](https://stackoverflow.com/questions/23526756/unable-to-dequeue-a-cell-with-identifier-cell-must-register-a-nib-or-a-class-f) – Vandit Mehta Feb 26 '18 at 12:06
  • I don't think it's a duplicate of that post, we're trying to find the solution to two different failures, which are both associated with prototype cells. Therefore, to summarize, our issues are different, therefore our solutions, in theory, should also be different. –  Feb 26 '18 at 15:19

1 Answers1

0

if you have created a xib for table view cell then you need to register like this..

UINib *nib1 = [UINib nibWithNibName:@"MyListingsTableViewCell" bundle:nil];
[self.tableView registerNib:nib1 forCellReuseIdentifier:@"MyListingsTableViewCell"];

If you have desired you cell in storyboard then you need to specify its custom class and cell reuse identifier...

enter image description here

enter image description here

When every you are working with custom cells then you need to one of the following....

  • Create a xib and register nib to tableview

or

  • Design cell in story board, set custom class and reuse cell identifier.

You did both the things...thats why it was crashing bcoz you are registering "MyListingsTableViewCell" nib to tableview that does not exits.

Connect all IBOutlest like this...

enter image description here

Set tableview delegate and datasource...

enter image description here

Or from code also you can set this like below in your MyListings class...

self.tableView.dataSource = self
self.tableView.delegate = self

Final output...its working fine from your code...

enter image description here

Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • then use `[tableView dequeueReusableCellWithIdentifier:@"your cell identifier" forIndexPath:indexPath];` to reuse cell. – Mahendra Feb 26 '18 at 12:37
  • Have I not already got this in my code? it reads "MyListingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingsTableViewCell" forIndexPath:indexPath];" –  Feb 26 '18 at 12:40
  • Then what is the issue you are facing now? – Mahendra Feb 26 '18 at 12:42
  • "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle:" –  Feb 26 '18 at 12:42
  • have you created a xib for cell or designed your cell in storyboard ? – Mahendra Feb 26 '18 at 12:44
  • I designed the cell in my storyboard, then made a class for a UITableViewCell that defines all the properties of this prototype cell I made. –  Feb 26 '18 at 12:47
  • Ok fine...now check have you set cell's custom class? – Mahendra Feb 26 '18 at 12:51
  • Yes, the class has been set to "MyListingsTableViewCell" –  Feb 26 '18 at 12:52
  • thats correct, so now check your Cell Reuse Identifier in story board...what you have written there? – Mahendra Feb 26 '18 at 12:53
  • ah, haha. It's had a lower case m this whole time... thanks for assisting me there. –  Feb 26 '18 at 12:54
  • The application still crashes with the error "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: " –  Feb 26 '18 at 12:56
  • Cell reuse identifier in storyboard and in `cellForRow` is same ? just copy paste it, if not... – Mahendra Feb 26 '18 at 12:59
  • Yes, it's the same, this time. I capitalised the reuse identifier, instead. As every time I call it, it's name is the same, apart from a capital "M" for my –  Feb 26 '18 at 13:00
  • Can you attach the screen shot of your custom class and cell reuse identifier from story board....as I have attached...see my answer... – Mahendra Feb 26 '18 at 13:02
  • Ok, it's my first time uploading a screenshot here... I HAVE to do it in my initial post, correct? –  Feb 26 '18 at 13:03
  • Ah, it says I need at least 10 reputation to post images here. Instead, here are the links: https://imgur.com/a/PXgTd and https://imgur.com/2PuUNUk –  Feb 26 '18 at 13:16
  • Your reuse identifier is correctly set (make sure there is no trailing space)....post image where you have specified custom class in storyboard (i.e. Identity inspector).....not the screen shot of code... – Mahendra Feb 26 '18 at 13:20
  • Ok, I've made sure there's no space. –  Feb 26 '18 at 13:21
  • Add screen shot where you have specified custom class in storyboard (i.e. Identity inspector) – Mahendra Feb 26 '18 at 13:22
  • Just do one thing....send me code on my email mahendrapatelg.it@gmail.com and Let me check the issue...if possible... – Mahendra Feb 26 '18 at 13:24
  • I've sent you an email with the relevant files attached. –  Feb 26 '18 at 13:29
  • Not to rush you, but I need a solution for this urgently. If it's not too much to ask for, can you get back to me ASAP, if you're aware of what I'm doing wrong or would prefer to show me an alternative method to achieve the same, desired, outcome? –  Feb 26 '18 at 14:32
  • I've checked your code....in MyListing.m file you have registered nib, just comment that line..... – Mahendra Feb 26 '18 at 15:33
  • Right on time, I've commented the line and changed "[self.tableView registerClass:[MyListingsTableViewCell class] forCellReuseIdentifier:@"MyListingsTableViewCell"];" this part of the code, too. The change I made was changing "UITableViewCell" to "MyListingsTableViewCell" –  Feb 26 '18 at 15:34
  • Okay, now, it no longer crashes, however it has a white background (which I can fix), doesn't match the dimensions of the prototype cell, at all, and doesn't have any text or anything inside it... –  Feb 26 '18 at 15:35
  • Do you know what was the actual problem...? – Mahendra Feb 26 '18 at 15:36
  • From what I now understand, the actual problem was that the code had no idea what "prototype" I was talking about, for the prototype cell(s) that I was trying to instantiate. –  Feb 26 '18 at 15:37
  • But yeah, the application is no longer crashing, it's just the table view cell isn't populated at all, by anything. –  Feb 26 '18 at 15:38
  • Ok, I understand this. Do you know why it's not populating the cells? Is my code all correct? Sorry to keep asking you for help. –  Feb 26 '18 at 15:42
  • Specifically, what's happening is that it's making 2 cells, which are blank, and, therefore, since I've now changed the background colour of the cells to blank, I can only identify that they're there when I click on them and they, therefore, become highlighted. So, there are 2 cells present, but they don't have the same size as the prototype cell and don't have any properties of the prototype cell, at all. –  Feb 26 '18 at 15:44
  • Here's a visualisation of the behaviour I'm trying to describe: https://imgur.com/a/YgCM2 –  Feb 26 '18 at 15:46
  • Ah, I connect EVERYTHING within the prototype cell, and the prototype cell itself, to the class that handles the view controller? –  Feb 26 '18 at 15:47
  • Shouldn't the external class I made handle all of the inputs into the fields? I implemented the method I had in my previous class that should satisfy this process, I don't understand why it doesn't work... –  Feb 26 '18 at 15:52
  • Potentially, could it be that I have to import the table view cells class into the view controllers class? –  Feb 26 '18 at 15:53
  • If you defined your cell in the Storyboard, then you do **not** need to register the class for your cell, since the Storyboard already did this for you. – Alejandro Iván Feb 26 '18 at 15:54
  • So, what exactly must I do to resolve this issue? Do I make outlets in my view controller's class for the cell itself and each component of the cell's contents? What function can I implement for this? –  Feb 26 '18 at 15:56
  • For me, with the exact same naming, I've connected those components to the class that I made for handling this tableviewcell –  Feb 26 '18 at 15:57
  • This is what my pre-existing connections look like: https://imgur.com/a/EoIuh (They link to the code snippet of the second class I included in my main body area of this comment thread). –  Feb 26 '18 at 16:02
  • Oh, sorry man, unfortunately, Skype got blocked in my country of residence a few weeks ago, it's not possible for me to use that service anymore... :\ –  Feb 26 '18 at 16:03
  • I have seen image and its correct all iboutlets are correctly connected. – Mahendra Feb 26 '18 at 16:04
  • Ok, but they are connected to my "MyListingsTableViewCell" class, not my view controller class. The behaviour observed by the current configuration is as shown in the second last set of imgur links. –  Feb 26 '18 at 16:05
  • Thats correct you need to connect to iboutlets to `MyListingsTableViewCell` class. – Mahendra Feb 26 '18 at 16:06
  • The datasource and delegate for the table view, in the storyboard, are pointing to the view controller and both of those have been imported into the .h file for the view controller. –  Feb 26 '18 at 16:10
  • No, I had already done these two things prior to my initial issue here... –  Feb 26 '18 at 16:12
  • Previously, I followed all of the steps presented in this tutorial: https://theappspace.com/create-dynamic-prototype-cells-in-uitableview-video-tutorial/ –  Feb 26 '18 at 16:15
  • I used the dragging widgets from the storyboard to set them, but imported the protocols in the .h file for the view controller, like in that video I attached. (It has no audio) –  Feb 26 '18 at 16:18
  • No no...I'm not asking about widgets or views.....I m asking to set datasource and delegate.. – Mahendra Feb 26 '18 at 16:21
  • I set those two in the storyboard –  Feb 26 '18 at 16:25
  • I set them to the view controller. In the storyboard, I attached them to the first item in the hierarchy, the one with orange squares inside an orange circle. –  Feb 26 '18 at 16:27
  • Yes, that's exactly how they're set. –  Feb 26 '18 at 16:30
  • So I'm not sure what's causing the cells to have an incorrect height + no properties. –  Feb 26 '18 at 16:32
  • for dynamic cell height...set `self.tableView.estimatedRowHeight = UITableViewAutomaticDimension` in MyListings.m – Mahendra Feb 26 '18 at 16:40
  • That didn't do anything, for me. It's as if it's ignoring the prototype cell entirely. –  Feb 26 '18 at 16:47
  • Haha, just as I have ideas and do stuff, my page always refreshes and you reply telling me to do what I did 2 seconds ago. This is great xD –  Feb 26 '18 at 16:50
  • Haha...so you already debugged it..so tell me it is called or not..?? – Mahendra Feb 26 '18 at 16:52
  • It's very strange, actually. It seems as though everything should be working. As the code goes downwards, it recognises that there are 2 elements in the arrays. Then, when it gets to the updateCellWithTitle function, it successfully shows the process of completing the function, by showing me that values are being placed in the .text components of the cells properties, in the other class. However, when we get to return cell, everything is null... –  Feb 26 '18 at 16:56
  • So, to clarify, it does recognise every single value, correctly, that is being passed to the properties of the instantiated cells. Which is good, however, it returns a blank cell... –  Feb 26 '18 at 16:57
  • Although the values that the .text properties are being equated to register as the ones in the array, the labels are all still returning "nil" values. –  Feb 26 '18 at 17:09
  • When I run the code, the values being passed to the .text properties never actually register. So the .text properties for all the labels are always nil, even though the variable they're being equated to show up in the debugger view. –  Feb 26 '18 at 17:19
  • Are you sure that our files are EXACTLY the same, maybe you added some additions that I might now have? Because my code is genuinely not working... –  Feb 26 '18 at 17:42
  • There are a few differences between our implementations, actually. My prototype cell had a height of approx. 3 of the regular cells. On top of this, it had properties that were in different locations within this cell. Even when I cut out the external class for this tableviewcell and set string values for the cells properties, the cells properties still don’t appear in the, somewhat, working table cell. On top of that, the cell still shows the ordinary size. –  Feb 26 '18 at 17:51
  • I know why the properties were ignored, it’s because of that reuse identifier that we declared in the view did load. Because we declared it there, it ignored the one in the function that places values in the cell. However, my last issue is that the height of the prototype cell is being ignored. ONLY the height, which is very strange... –  Feb 26 '18 at 18:51