6
votes

I have a problem in my storyboard.It is working fine but when i try to change the content property of UITableView it caused following error

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m 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 want to design a grouped tableview with static cell.Thanks in advance

Code

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 2; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return 3; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
return cell; 
} 
7
if you don't share your code, we wont be able to help you - Raheel Sadiq
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; return cell; } - Adnan Chaudhry
@AdnanChaudhry : add the code to the question,You can edit the question and add the code like this..see the edit option under the question - Lithu T.V
See the link for more information. - Rajal

7 Answers

7
votes

if you use static cell, just comment all the UITableViewDatasourceDelegate method. so comment the following code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 2; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return 3; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
return cell; 
} 

Wish this will help you!

7
votes

Instead of using:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//-> This is used when using prototype cells (e.g) Dynamic TableView

Use:

UITableViewCell *cell = [super tableView:tableView
                   cellForRowAtIndexPath:indexPath];

//-> Since you chose Static TableCells you don't need to reuse instead use the cells you created in nib

1
votes

I have encountered same issue, and I think I found the way to work this out.

  1. Create a Controller extends from UITableViewController to wrap its controller, it will have default UITableViewDelegate and UITableViewDataSource methods.
  2. Remove UITableView delegate and datasource
  3. Delete default UITableViewDelegate and UITableViewDataSource methods from your controller file, coz it's static cells, so if these exist, will not appear

Hope it helps.

0
votes

When using storyboard the tableview cell must be registered in the storyboard.That means cell must be added to the table and its identifier must be set.This identifier should be used in the code in cellForRowAtIndexpath

EDIT

In case of static cells the each purticular cells needed to be created in the nib and filled content via code or nib

Read this

0
votes

You are not allowed to use static cells in a normal UITableView.

Instead you need to use UITableViewController to go with the static cells.

this and this may or might help.

0
votes
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [YourArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = nil;
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}
0
votes

Add this code in cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
else
{
    UIView *subview;
    while ((subview= [[[cell contentView]subviews]lastObject])!=nil)
        [subview removeFromSuperview];
}