{"id":6891,"date":"2014-05-09T04:24:41","date_gmt":"2014-05-09T04:24:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/09\/strange-error-insertrowsatindexpaths-in-uitableview-crashes-with-nsinternalinconsistencyexception-collection-of-common-programming-errors\/"},"modified":"2014-05-09T04:24:41","modified_gmt":"2014-05-09T04:24:41","slug":"strange-error-insertrowsatindexpaths-in-uitableview-crashes-with-nsinternalinconsistencyexception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/09\/strange-error-insertrowsatindexpaths-in-uitableview-crashes-with-nsinternalinconsistencyexception-collection-of-common-programming-errors\/","title":{"rendered":"Strange error: insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException-Collection of common programming errors"},"content":{"rendered":"<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>\n<pre><code>** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to resolve row for index path:  2 indexes [0, 1]'<\/code><\/pre>\n<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>\n<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>\n<pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section\n<\/code><\/pre>\n<p>returns the correct number of rows in a section after a change.<\/p>\n<p>What am I doing wrong in my code to get the above mentioned exception?<\/p>\n<p>This is the interface file:<\/p>\n<pre><code>#import  \n#import \n\n@interface MasterViewController : UITableViewController {\n  NSMutableArray *rowsInSection;\n  NSMutableArray *cellForRow;\n}\n\n@property (nonatomic,strong) NSMutableArray *rowsInSection;\n@property (nonatomic,strong) NSMutableArray *cellForRow;\n\n@end\n<\/code><\/pre>\n<p>And this is the implementation file:<\/p>\n<pre><code>#import \"MasterViewController.h\"\n\nconst NSInteger numSections = 4;\nconst NSInteger numRows = 1 + 4;\nconst NSInteger addRemoveRows = 4;\n\n@implementation MasterViewController\n\n@synthesize rowsInSection;\n@synthesize cellForRow;\n\n- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {\n    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];\n\n    if (self) {\n\n        self.title = @\"Table View\";\n        rowsInSection = [NSMutableArray arrayWithCapacity:numSections];\n        cellForRow = [NSMutableArray arrayWithCapacity:numSections];\n    }\n    return self;\n}\n\n\n#pragma mark - View lifecycle\n\n- (void)viewDidLoad {\n    [super viewDidLoad];\n\n    \/\/ Do any additional setup after loading the view, typically from a nib.\n\n    self.tableView.backgroundColor = [UIColor clearColor];\n    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;\n    self.tableView.dataSource = self;\n    self.tableView.delegate = self;\n\n    \/\/ add number of rows for section\n    for (NSInteger i = 0; i &lt; numSections; i++) {\n        [self.rowsInSection addObject:[NSNumber numberWithInteger:1]];\n    }\n\n    \/\/ container for reusable table view cells\n    for (NSInteger i = 0; i &lt; numSections; i++) {\n\n        NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:numRows];\n\n        for (NSInteger j = 0; j &lt; numRows; j++) {\n\n            \/\/ top row in section\n            if (j == 0) {\n                UITableViewCell *topCell = [[UITableViewCell alloc] \n                                            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];\n                topCell.accessoryType = UITableViewCellAccessoryNone;\n                topCell.contentView.backgroundColor = [UIColor whiteColor];\n                topCell.textLabel.textColor = [UIColor blueColor];\n                [rowsArray addObject:topCell];\n\n                \/\/ the rest\n            } else {\n                UITableViewCell *simpleCell = [[UITableViewCell alloc] \n                                               initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];\n                simpleCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;\n                simpleCell.textLabel.textColor = [UIColor whiteColor];\n                [rowsArray addObject:simpleCell];\n            }\n        }\n\n        \/\/ add rows for current section into cell container\n        [self.cellForRow addObject:rowsArray];\n    }\n\n}\n\n- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {\n    \/\/ Return YES for supported orientations\n    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);\n}\n\n\/\/ Customize the number of sections in the table view.\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {\n    return numSections;\n}\n\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {\n\n    NSInteger rows = [(NSNumber *)[self.rowsInSection objectAtIndex:section] integerValue];\n\n    \/\/NSLog(@\"%@\",self.rowsInSection);\n    \/\/NSLog(@\"Rows: %d in section: %d\",rows,section);\n\n    return rows;\n}\n\n\/\/ Customize the appearance of table view cells.\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {\n\n    \/\/ Configure the cell.\n\n    \/\/ row count\n    NSLog(@\"Rows: %d in section: %d\",[tableView numberOfRowsInSection:indexPath.section],indexPath.section);\n\n\n    if (indexPath.row == 0) {\n        UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];\n        cell.textLabel.text = @\"TOP ROW\";\n        NSLog(@\"Row: %d in section: %d - %@\",indexPath.row,indexPath.section,cell);\n        return cell;\n    } else {\n        UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];\n        cell.textLabel.text = [NSString stringWithFormat:@\"row: %d\",indexPath.row];\n        NSLog(@\"Row: %d in section: %d - %@\",indexPath.row,indexPath.section,cell);\n        return cell;\n    }\n\n    \/\/ not reaching here\n    return nil;\n}\n\n- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {\n    return [NSString stringWithFormat:@\"Section %d\",section];\n}\n\n\n#pragma mark - Row editing\n\n- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {\n\n    \/\/ add table view cells to section if tapped on top row\n    if (indexPath.row == 0 &amp;&amp; [tableView numberOfRowsInSection:indexPath.section] == 1) {\n\n        \/\/NSLog(@\"Selected row: %d in section: %d\",indexPath.row,indexPath.section);\n\n        NSMutableArray *indexPathArray = [NSMutableArray array];\n\n        for (NSInteger i = 1; i<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have searched for a more fitting answer to NSInternalInconsistencyException 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6891","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6891"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6891\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}