Adnan Chaudhry
Adnan Chaudhry

Reputation: 91

UITableView Content "Static cell" not working in iOS7

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; 
} 

Upvotes: 6

Views: 11411

Answers (7)

Ankit Goyal
Ankit Goyal

Reputation: 3049

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];
}

Upvotes: 0

Panfilo Mariano
Panfilo Mariano

Reputation: 79

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

Upvotes: 7

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

- (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;
}

Upvotes: 0

pingguoilove
pingguoilove

Reputation: 109

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!

Upvotes: 7

Saru
Saru

Reputation: 861

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.

Upvotes: 0

Tom
Tom

Reputation: 4662

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.

Upvotes: 1

Lithu T.V
Lithu T.V

Reputation: 20021

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

Upvotes: 0

Related Questions