Assertion failure in UITableView configureCellForDisplay:forIndexPath:

IosObjective CCocoa TouchUitableview

Ios Problem Overview


I'm not sure where the error is here, having looked at other similar issues. I received an Assertion failure.

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

I think it is something simple but hope someone can help.

Below is my code:

#import "StockMarketViewController.h"

@interface StockMarketViewController ()

@end


@implementation StockMarketViewController
@synthesize ShareNameText, ShareValueText, AmountText;
@synthesize shares, shareValues;


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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
   
    
    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
    
}

Ios Solutions


Solution 1 - Ios

you are never creating a cell, you just try to reuse a dequeued cell. but as you never created one, there is none.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
}

or try (only iOS 6+)

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

    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
}

from UITableView.h

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier 
                           forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

-dequeueReusableCellWithIdentifier: will always need a check, if a cell was returned, while
-dequeueReusableCellWithIdentifier:forIndexPath: can instantiate new one.

Solution 2 - Ios

If you have not defined a prototype cell with the identifier @"cell" in Storyboard, you will get an assertion error when you attempt to dequeue it.

You can fix this by setting the Identifier property on the prototype cell (select the cell and set that attribute in the right hand panel).

Solution 3 - Ios

A very silly mistake i had done was

i didn't put the UITableViewDelegate, UITableViewDataSource after the controller class name like my class code was class TagsViewController: UIViewController

it should have class TagsViewController: UIViewController , UITableViewDelegate, UITableViewDataSource

May be one of you is facing due to this all other code was ok.

Solution 4 - Ios

You need to call "initWithStyle" in custom TableViewCell and initialise the objects again.

Example: ProductTableViewCell.m file

@implementation ProductTableViewCell

- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
   [super setSelected:selected animated:animated];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(70, 0, 320, 60))];
        [self.contentView addSubview:_titleLabel];
   }
   return self;
}

In the main implementation file

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productTableViewCell"];
    NSDictionary *dic = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        dic = [_filteredArray objectAtIndex:indexPath.row];
    } else {
        dic = [_originalArray objectAtIndex:indexPath.row];
    }
    cell.titleLabel.text = [dic objectForKey: @"title"];
    return cell;
}

Solution 5 - Ios

I had the same error, and I managed to find the fault. I had an array for the segues and view titles:

NSArray *MMTitles= [NSArray arrayWithObjects:@"MainMenu",@"viewIt",@"viewNots",@"MyProfile",@"Settings",@"Instructions",@"Help", nil];
NSArray *MMSegues=[NSArray arrayWithObjects:@"MainMenu",@"MyProfileSegue",@"viewNotSegue",@"MyProfileSegue",@"SettingsTableViewSegue",@"InstructionsViewSegue",@"HelpViewSegue", nil];

self.menuItems = [[NSArray alloc]initWithObjects:MMTitles,MMSegues, nil];

I then used this array as the datasource for my table. The error I was receiving was due to the fact that I didn't in fact had the HelpViewSegue declared in my Storyboard when I instantiated the VC:

    vc = [mainStoryboard instantiateViewControllerWithIdentifier: [[self.menuItems objectAtIndex:1]objectAtIndex:indexPath.row]];

Pretty trivial, but it was pretty frustrating! Hope this helped.

Solution 6 - Ios

In the below code you have written @"cell" (written with a small c), but you have to use @"Cell" (C must be capital).

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason TaylorView Question on Stackoverflow
Solution 1 - IosvikingosegundoView Answer on Stackoverflow
Solution 2 - IossapiView Answer on Stackoverflow
Solution 3 - IosOurangZeb KhanView Answer on Stackoverflow
Solution 4 - Iosuser1802778View Answer on Stackoverflow
Solution 5 - IosSeptronicView Answer on Stackoverflow
Solution 6 - IosChowdaryView Answer on Stackoverflow