Killian
Killian

Reputation: 934

unable to dequeue a cell with identifier tag

I'm getting an error when a button press loads the class posted below.

The code is supposed to load a slide out menu. This is the line that is causing my issues. I'm completely new to iOS / obj-c. I'm not sure why, but the method this line of code is in, loops through for each entry in the _menuItems array? The NSLog outputs for each item of the array, but then it runs another time and throws this error? Thats what I think is happening at least. If anyone could give me some pointers, I'd be grateful. Aside to all that, my project has two targets - I don't know why, I don't know how, and I don't know how to change it. Could that be the issue?

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

The console printout is this:

013-11-28 12:11:31.902 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.908 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.911 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.917 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.921 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.924 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.929 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.931 DatabaseTest[57858:a0b] The code runs through here!
2013-11-28 12:11:31.932 DatabaseTest[57858:a0b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:5261
2013-11-28 12:11:31.936 DatabaseTest[57858:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier tag - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

.

#import "SidebarViewController.h"
#import "SWRevealViewController.h"

@interface SidebarViewController ()

@property (nonatomic, strong) NSArray *menuItems;
@end

@implementation SidebarViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
    self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];

    _menuItems = @[@"title", @"news", @"comments", @"map", @"calendar", @"wishlist", @"bookmark", @"tag"];

}

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
    // Set the title of navigation bar by using the menu items
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
    destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];


    if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
        SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;

        swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {

            UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
            [navController setViewControllers: @[dvc] animated: NO ];
            [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
        };

    }

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.menuItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
    NSLog(@"The code runs through here!");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

@end

Upvotes: 0

Views: 1881

Answers (3)

milguad
milguad

Reputation: 7

I am not sure if you still after the answer for this question, but if you are here is the way you could solve your problem.

Your problem arises from the fact that the cell hasn't got an identifier or if it does, the identifier doesn't match the code in the NSArray *menuItems. To solve the problem:

  • you should go to the Document Outline (The small arrow at the bottom left of your storyboard)
  • then you should click on the 'Table View Cell - tag. (It should highlight the cell 'Tag'
  • Go to the Attributes Inspector and find 'Identifier'

  • Finally, change or ad the Identifier name, in your case 'tag'.

  • Et voila!, that should do the trick

Beware of spelling as Objective C is very tight on spelling, I mean you have to make sure the same way you spell the identifier, you must use the same spelling in your code.

Hope that help,

See picture: (I wanted to post one for you, but it seems like I cannot do so, Ooops!)

Upvotes: 0

Eugene P
Eugene P

Reputation: 572

You must use

static NSString *CellIdentifier = @"messageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

Do not use if your cell is not registered a class or nib with Cell ID;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];

Apple Documentation:

Important

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

Edited:

Upvotes: 1

Abizern
Abizern

Reputation: 150605

This is wrong:

NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];

Unless you have registered a cell for each row this isn't going to work.

You need to use a cell identifier that has been registered either in code or from the storyboard / xib.

also - the way you are doing this now takes no advantage of cell reuse if each cell loads a cell with a unique identifier.

Upvotes: 0

Related Questions