Luka Jashiashvili
Luka Jashiashvili

Reputation: 41

iOS - Objective-C unable to dequeue a cell with identifier

'unable to dequeue a cell with identifier news - 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


    NSArray *menuItems;



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

- (void)viewDidLoad
{
    [super viewDidLoad];

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


}

- (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 menuItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

@end

Identifier looks like:

enter image description here

what can i do with this?

i'm trying to add left side slide menu, but it crashes when i push menu button.

Upvotes: 4

Views: 1607

Answers (2)

Jef
Jef

Reputation: 4728

You are actually very close, and your problem is a source of frustration for many new developers. There are two different, mutually exclusive (competing?) APIs for dequeueReusableCell and you have accidentally mixed them together.

1.

- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier 

introduced in iOs2.0.
This method is capable of returning nil, and when it does it is up to you to create the cell.

2.

- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

introduced iOs6.0 This method should not be capable of returning nil. It requires you to register a UITableViewCell class and identifier for each index path in the table, and then the cells are created 'automagically' for you. (This coincided with the introduction of UICollectionView and it is how those work)

Solution. You are using the first approach (which is fine) but you've used the method from the second. alter your cellForRow... method to something like this:

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

    NSString *CellIdentifier = @"someStaticString" ; 
    //btw, it is poor practice to name variables with a capital. Save that for classes/types
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[MYcellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; 
    }
    //configure cell
    return cell;
}

or... register your cell class / prototype and identifier combo with the tableView (yes, you can use the same setting for all indices..) and then leave your code as it is.

IMHO this is a very common mistake and something that is not documented terribly well, only guys who've been developing since before iOs6 and the introduction of the second approach see the difference without help. Best.

Upvotes: 2

Artyom Devyatov
Artyom Devyatov

Reputation: 1054

All your cells in given example should have the same identifier because they are very similar. Icon+text is the content of your cell. Cell identifier describe common structure of cell. So, you have to create one identifier for all your cells and set icon and title in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

Upvotes: 0

Related Questions