Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView with two types of resusable cell not working properly
    primarykey
    data
    text
    <p>I am trying to create a table view which uses two types of cells - default and subtitle. </p> <p>I tried to use a single reusable cell (<code>*cell</code>) 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. </p> <p>I thought that I could try to add a second type of cell (<code>*cellB</code>) and when I did it seemed to solve the problem however it would crash every so often with the following error:</p> <pre><code>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' </code></pre> <p>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> <p>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.</p> <p>Thanks in advance. </p> <pre><code>- (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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload