Runtime error after writing reusable cell code


#1

Hi people,

I have been cracking my head where went wrong. I could not run the program and am currently facing the error below.

Hope you can give me some guidance so i can move on from here. Thanks!

2014-08-24 22:04:43.463 Homepwner[8031:60b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘must pass a class of kind UITableViewCell’

#import “BNRItemsViewController.h”
#import “BNRItem.h”
#import “BNRItemStore.h”

@implementation BNRItemsViewController

//create designated initialiser
-(instancetype)init
{
//always conform to 2 rules below
//call superclass designated initaliser
self = [super initWithStyle:UITableViewStylePlain];

if(self) {
    for(int i=0; i<5; i++) {
        [[BNRItemStore sharedStore] createItem];
    }
}
return self;

}

//designated initialiser overriden
-(instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}

//method overriden
-(void)viewDidLoad
{
[super viewDidLoad];

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

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [[[BNRItemStore sharedStore] allItems] count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //get a new or recycle cell
    //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
    // reuseIdentifier:@“UITableViewCell”];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];

    //set the text of the cell with the description of the item
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    BNRItem *item = items[indexPath.row];

    cell.textLabel.text = [item description];

    return cell;
    }


#2

[quote][code]- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//get a new or recycle cell
//UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
// reuseIdentifier:@“UITableViewCell”];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];

//set the text of the cell with the description of the item
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];

cell.textLabel.text = [item description];

return cell;
}[/code][/quote]

[quote]... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; ... [/quote]
So, what do you do if there are no reusable cells?


#3

I am following what the book says. I don’t quite understand the error so I hope you can guide me here a little.

If I don’t have reusable cell I would need to create new cell isn’t it


#4

It’s just a typo. In -viewDidLoad, you have:

[self.tableView registerClass:[UITableView class] forCellReuseIdentifier:@"UITableViewCell"];
It should be:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
Note the use of UITableView[color=#FF0000]Cell[/color] on the first line.

Incidentally…it would be MUCH easier to read the code when you copy-and-paste it into this forum if you first tap the “Code” button at the top of the web text field, and paste it between the resulting [ code] and [ /code] tags. (Spaces added here to make the tags show.)


#5

Good catch, gc3182!

Also, better to define a read-only name for “UITableViewCell” and use that name rather then typing in “UITableViewCell”:

NSString *const CellID = @"UITableViewCell";
...
[self.tableView registerClass:[UITableViewCell class]
       forCellReuseIdentifier:CellID];
...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
assert (cell);
...

#6

Omg!!! Thank you so much!!!


#7

Hello All,

I’m experiencing a similar problem. When I run my code I get the following error:
Homepwner[3231:35381] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘must pass a class of kind UITableViewHeaderFooterView’

//
//  BNRItemsViewController.m
//  Homepwner
//
//  Created by KB on 2014-09-30.
//  Copyright (c) 2014 DC Inc. All rights reserved.
//

#import "BNRItemsViewController.h"

@implementation BNRItemsViewController
-(instancetype)init{
    // Call the super classes's designated initialzer method first
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        for (int i=0; i<5; i++) {
            [[BNRItemStore sharedStore]createItem];
        }
    }
    return self;
}
-(instancetype)initWithStyle:(UITableViewStyle)style{
    return [self init];
}

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

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // Get a new or recycled cell
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    // Set the text on the cell with the description of the item that is at the nth index of items, where n = row this cell will appear in on the table view
    NSArray * items = [[BNRItemStore sharedStore] allItems];
    BNRItem * item = items[indexPath.row];
    cell.textLabel.text = [item description];
    return cell;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forHeaderFooterViewReuseIdentifier:@"UITableViewCell"];
}
@end

Please advise??


#8

I’m guessing you just accepted the code that Xcode offered to auto-complete for you.

You have:

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

But if you read just a couple posts prior, you’ll see:

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

Gotta read closely…