0
votes

I am trying to transition from a view controller to a table view controller programmatically (vs. with a storyboard segue), using this code:

[self.navigationController pushViewController:photosTVC animated:YES];

However, I get an "assertion error" when the table view controller is loading; specifically, on the second line of this method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Photo";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
...

The table view controller loads fine when transitioning to it using a segue, but just not when I transition this way. Any ideas I could try would be much appreciated!

Thanks for reading.

EDIT: The full error text is below:

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460

1
What is the complete error?rmaddy

1 Answers

1
votes

To use [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; the cell has to be registered using registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: which is done for you if you're using storyboard.

So use just this method, [tableView dequeueReusableCellWithIdentifier:CellIdentifier] when you're doing it programatically unless there are specific reasons not to.

EDIT:

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    // or whatever cell initialisation method you have / created
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}