Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:40:38+00:00 2026-05-27T11:40:38+00:00

I am trying to create a table view which uses two types of cells

  • 0

I am trying to create a table view which uses two types of cells – default and subtitle.

I tried to use a single reusable cell (*cell) which seemed to work fine until I got to the bottom cell which was off screen – when this came into view it was a duplicate of the first visible cell.

I thought that I could try to add a second type of cell (*cellB) and when I did it seemed to solve the problem however it would crash every so often with the following error:

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

I know that I am doing something wrong and I am pretty sure I am not implementing the reusable cells properly but after several days of frustration I would really appreciate some advice.

P.S. I have searched many previous posts but none seem to cover the issue of using two types of cells on the same table.

Thanks in advance.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return  5; 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

 UITableViewCell *cellB = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellB"];


switch (indexPath.section) {


 case 0:
     if (cell == nil) {
         // The only subtitle cell
         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"UITableViewCell"]; 

     }

        [[cell textLabel] setText:title];
        [[cell textLabel] setTextColor:[UIColor whiteColor]]; 
        [[cell detailTextLabel] setText:[NSString stringWithFormat:@"Entry: £%@",price]];
        [[cell detailTextLabel] setTextColor:[UIColor colorWithWhite:1.0 alpha:.8]]; 


        cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     break;

 case 1:
     if (cell == nil) {

         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"UITableViewCell"]; 

         UIView *cellBackView = [[UIView alloc] initWithFrame:CGRectZero];
         cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"PhotoFrame.png"]];
         cell.backgroundView = cellBackView;

     }

     [lImage setFrame:CGRectMake(0, 23, 320, 200)];

     [cell.contentView addSubview:lImage];

     break;

 case 2:
     if (cell == nil) {

         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"UITableViewCell"]; 

         UIView *cellBackView = [[UIView alloc] initWithFrame:CGRectZero];
         cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"PaperTop.png"]];
         cell.backgroundView = cellBackView;

     }

     break;

 case 3:
     if (cell == nil) {

         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"UITableViewCell"]; 


         UIView *cellBackView = [[UIView alloc] initWithFrame:CGRectZero];
         cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"Paper.png"]];
         cell.backgroundView = cellBackView;

     }

     break;

 case 4:
     // I'm pretty sure this bit is wrong but if I use (cell == nil) the first cell is shown instead
     if (cellB == nil) {

         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"UITableViewCellB"]; 

         UIView *cellBackView = [[UIView alloc] initWithFrame:CGRectZero];
         cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"PaperBottom.png"]];
         cell.backgroundView = cellBackView;

     }

     break;

 default:
     break;
} 

 return cell; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:40:39+00:00Added an answer on May 27, 2026 at 11:40 am

    As cgull pointed out, when [indexPath section] is 4, you’re initializing cellB, but still returning cell, which may or may not have a valid cell in it.

    You need to have one variable that holds the cell you’re going to return, no matter the path taken through the switch:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        NSString identifier = @"UITableViewCell";
        if( [indexPath section] == 4 ){
            identifier = @"UITableViewCellB";
        }
        // Try to dequeue the appropriate cell kind
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    
        switch (indexPath.section) {
    
                //other cases...
    
            case 4:
                // You now have only one variable that can possibly hold a cell.
                // If section is 4, it's either nil or a UITableViewCellB kind.
                if (cell == nil) {
    
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"UITableViewCellB"];
                    // Set up new cell
    
                }
    
                break;
    
            default:
                break;
        } 
    
        return cell; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create 3 table view cells using code (w/o nib). I
I am trying to create a table view which has a layout like what
I am trying to create a pivot table type view in postgresql and am
I'm trying to create a table with two columns comprising the primary key in
I'm trying to create a subset of a table (as a materialized view), defined
In a MVC project am trying to create a view which has the Title
I'm trying to create a page using ASP.NET MVC 2 which shows two things:
I am trying to create a table with the following: CREATE TABLE GTW_WORKFLOW_MON (
I am trying to create a table though prepare statement but it is giving
I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.