Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

medasmx

macrumors member
Original poster
Nov 9, 2008
89
0
Below is some code I cannot get to work (it is a table view with deletable rows). Basically, "myArray" is an array of elements "1","2", and "3". Name is a second class that I created that essentially consists of a single string. When I run the program I get the following error:

"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (17) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'"


Code:
-(NSMutableArray*)myArray
{
	if (!_myArray) {
		_myArray=[[NSMutableArray alloc]init];
	}
	int i;
	for (i=1; i<4; i++) {
		NSString *someString = [NSString stringWithFormat:@"%d", i];
		Name*secondName=[Name nameWithFirst:someString];
		[_myArray addObject:secondName];
	}
	return _myArray;
}

#pragma mark -
#pragma mark Table View Data Source Methods

-(NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section
{
	return [self.myArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
	
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
	}
	
	NSUInteger row=[indexPath row];
	Name*someOtherName=[self.myArray objectAtIndex:row];
	cell.textLabel.text=[someOtherName first];
	
	return cell;
}

- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.myArray removeObjectAtIndex:[indexPath row]];
        
        //  Animate deletion
        NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
        [[self tableView] deleteRowsAtIndexPaths:indexPaths
                                withRowAnimation:UITableViewRowAnimationFade];
    }
}

The funny thing is that when I define the array more simply, not using a separate class ("Name"), it works fine. Below is the version of myArray that works without error.

Code:
-(NSMutableArray*)myArray
{
	if(_myArray==nil)
	{_myArray=[[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three",nil];}
	return _myArray;
}

Thanks in advance for any help.

Adam
 
"...The number of rows contained in an existing section after the update (17) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'"

That message is indicates rather clearly that the _myArray has more elements than expected. Consider what happens when method 'myArray' is called for a second time.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.