Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange error: insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException
    primarykey
    data
    text
    <p>I have searched for a more fitting answer to <code>NSInternalInconsistencyException</code> I receive in the following sample app I wrote, but still nothing. The goal is to create an expand/collapse functionality for the top row in each section of the tableView. Right now I try to implement the expand part, and this works for row 0 in section 0. As soon as the user taps row 0 in another section this error appears:</p> <p></p> <p><pre><code>** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to resolve row for index path: 2 indexes [0, 1]'</pre></code></p> <p></p> <p>This is strange since I store each and every <code>UITableViewCell</code> for the table in a mutable array of arrays. <code>NSMutableArray *cellForRow</code>, where each index represents a section in the table and each object is an object of type <code>NSMutableArray</code>. I do this to avoid any issues arising from queueing reusable cells that I first thought triggered the above exception.</p> <p></p> <p>The exception happens at the <code>insertRowsAtIndexPaths</code> statement. I read earlier here that the <code>UITableViewController</code> code must keep track of changes to the number of rows caused by insertions/deletion. I believe I do that with <code>NSMutableArray *rowsInSection</code> so that the <code>UITableView</code> data source method:</p> <p></p> <p><pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section </pre></code></p> <p></p> <p>returns the correct number of rows in a section after a change.</p> <p></p> <p>What am I doing wrong in my code to get the above mentioned exception?</p> <p></p> <hr> <p>This is the interface file:</p> <p></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; @interface MasterViewController : UITableViewController { NSMutableArray *rowsInSection; NSMutableArray *cellForRow; } @property (nonatomic,strong) NSMutableArray *rowsInSection; @property (nonatomic,strong) NSMutableArray *cellForRow; @end </code></pre> <p></p> <p>And this is the implementation file:</p> <p></p> <pre><code>#import "MasterViewController.h" const NSInteger numSections = 4; const NSInteger numRows = 1 + 4; const NSInteger addRemoveRows = 4; @implementation MasterViewController @synthesize rowsInSection; @synthesize cellForRow; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"Table View"; rowsInSection = [NSMutableArray arrayWithCapacity:numSections]; cellForRow = [NSMutableArray arrayWithCapacity:numSections]; } return self; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.dataSource = self; self.tableView.delegate = self; // add number of rows for section for (NSInteger i = 0; i &lt; numSections; i++) { [self.rowsInSection addObject:[NSNumber numberWithInteger:1]]; } // container for reusable table view cells for (NSInteger i = 0; i &lt; numSections; i++) { NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:numRows]; for (NSInteger j = 0; j &lt; numRows; j++) { // top row in section if (j == 0) { UITableViewCell *topCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; topCell.accessoryType = UITableViewCellAccessoryNone; topCell.contentView.backgroundColor = [UIColor whiteColor]; topCell.textLabel.textColor = [UIColor blueColor]; [rowsArray addObject:topCell]; // the rest } else { UITableViewCell *simpleCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; simpleCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; simpleCell.textLabel.textColor = [UIColor whiteColor]; [rowsArray addObject:simpleCell]; } } // add rows for current section into cell container [self.cellForRow addObject:rowsArray]; } } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return numSections; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger rows = [(NSNumber *)[self.rowsInSection objectAtIndex:section] integerValue]; //NSLog(@"%@",self.rowsInSection); //NSLog(@"Rows: %d in section: %d",rows,section); return rows; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Configure the cell. // row count NSLog(@"Rows: %d in section: %d",[tableView numberOfRowsInSection:indexPath.section],indexPath.section); if (indexPath.row == 0) { UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; cell.textLabel.text = @"TOP ROW"; NSLog(@"Row: %d in section: %d - %@",indexPath.row,indexPath.section,cell); return cell; } else { UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"row: %d",indexPath.row]; NSLog(@"Row: %d in section: %d - %@",indexPath.row,indexPath.section,cell); return cell; } // not reaching here return nil; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringWithFormat:@"Section %d",section]; } #pragma mark - Row editing - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // add table view cells to section if tapped on top row if (indexPath.row == 0 &amp;&amp; [tableView numberOfRowsInSection:indexPath.section] == 1) { //NSLog(@"Selected row: %d in section: %d",indexPath.row,indexPath.section); NSMutableArray *indexPathArray = [NSMutableArray array]; for (NSInteger i = 1; i &lt;= addRemoveRows; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; [indexPathArray addObject:indexPath]; } // update row count for section NSInteger newRowCount = addRemoveRows + 1; // +1 for existing top row [self.rowsInSection replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInteger:newRowCount]]; [tableView beginUpdates]; [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; [tableView endUpdates]; } } @end </code></pre>
    singulars
    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.
    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.
 

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