Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cellForRowAtIndexPath: returns nil for a generic prototype cell

This is driving me nuts!

I have a generic UITableViewController class with a generic prototype cell, with an identifier "myCell". Developing under ARC, iOS5 and using Storyboard, I am using the following method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    cell.textLabel.text = @"Title";
    return cell;
}

Everything is hooked up in the storyboard, file owner, etc. I have several other UITableViewController in my project, using the same concept. All are working. This particular class of UITableViewController doesn't want to work! keep throwing me the exception:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Nslog'ed --> [tableView dequeueReusableCellWithIdentifier:@"myCell"] = (null)

Any idea and help is greatly appreciated!

EDIT:

For simplicity: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; }

Things that I have already done: 1. delete and create and set up the UITableViewController in storyboard! 2. Restarted Xcode 3. Restarted simulator 4. Restarted computer!!!! 5. Deleted and built the app over and over! None worked.

By the way, I tried

if (cell == nil)
 cell = [UITableViewCell alloc] initWithStyle....

.. and it will solve the problem, but again... this is ARC, storyboard, ... I am not supposed to do that. Xcode is supposed to take care of it!

like image 634
Canopus Avatar asked Jan 02 '12 16:01

Canopus


1 Answers

You have to create your cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"] autorelease];
    }

    cell.textLabel.text = @"Title";
    return cell;
}
like image 113
Tomasz Wojtkowiak Avatar answered Nov 09 '22 09:11

Tomasz Wojtkowiak