ankakusu
ankakusu

Reputation: 1828

Exception at cellForRowAtIndexPath function - UITableView

I want to display a list of data on "UITableView".

When I run the code I get the following error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 
'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'

I get the error given above when I run the code below. The error is thrown at the cellForRowAtIndexPath function. How can I solve this error?

@interface TheViewController ()

@property (strong, nonatomic) NSMutableArray *myTData;
@property (weak, nonatomic) IBOutlet UITableView *tableViewOutlet;

@end

@implementation TheViewController
- (void)viewDidLoad
{
    self.tableViewOutlet.delegate = self;
    self.tableViewOutlet.dataSource = self;
    self.tableViewOutlet.scrollEnabled = YES;
    self.myData = [[NSMutableArray alloc] initWithObjects:@"obj1", @"obj2", nil];
    [self.tableViewOutlet reloadData];
    [super viewDidLoad];
}

#pragma mark UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return myData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hede"];
    }
    [cell.textLabel setText: [myData objectAtIndex:indexPath.row]];
    [cell.detailTextLabel setText:@"hede hodo"];
    //
// Configure the cell..
    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

@end

Upvotes: 0

Views: 381

Answers (2)

Holly
Holly

Reputation: 5290

The exception is exactly what it says it is.

In iOS 5, you would call [tableView dequeueReusableCellWithIdentifier:CellIdentifier] and if it didn't return one, you would create one. The new call -dequeueReusableCellWithIdentifier:forIndexPath: should have a NIB or cell class registered and it will then always succeed.

You should call either

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

or

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

in your -viewDidLoad method.

Upvotes: 1

RP Development
RP Development

Reputation: 120

In your storyboard, select the prototype cell, go to inspector and give the cell the identifier "Cell" or whatever you want as long as you have the same here : static NSString *CellIdentifier = @"Cell";

Upvotes: 0

Related Questions