Search code examples
xcode4uitableviewstoryboardautomatic-ref-counting

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!


Solution

  • Eventually I found the bug. I can't say I found the root cause, but investigating line by line, this is what I found and worked for me.

    I have some objects in my UITableViewController that need to be alloc/init'ed before the view gets loaded, so that when callers can set them to pre-determind values. Since viewDidLoad is too late, I put them in initWithCoder method.

    Commeting out and re-writing the initwithCoder method solved the problem. It seemed to me, that initWithCoder method was initing the UITableViewController as some different!