Assertion failure in UITableView


#1

I’m to the point where I have an empty Homepwner list and I press the edit button and then the green insert button to add a new Possession and the code fails with this message:

2011-05-24 14:51:13.299 Homepwner[11557:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995 2011-05-24 14:51:13.650 Homepwner[11557:207] *** 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 (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'

It appears be coming from this section of code in the ItemsViewController

} else if (editingStyle == UITableViewCellEditingStyleInsert) { // If the editingstyle of the row was insertion, // we add a new possession object and new row to the table view [possesions addObject:[Possession randomPossession]]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

the debugger specifically says the thread received a SIGABRT in the last line.


#2

Hi,

Does your number of rows method have the numberOfRows++ line ?

[code]- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int numberOfRows = [possessions count];
// If we are editing, we will have one more row than we have possessions
if ([self isEditing])
numberOfRows++;

return numberOfRows;

}
[/code]

Gareth


#3

Yes, my tableView:numberOfRowsInSection: looks like

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int numberOfRows = [possesions count]; // If we are in editing, we will have one more row than we have possessions if ([self isEditing]) { numberOfRows++; } return numberOfRows; }


#4

I think the problem might be in the hand off from the HomepwnerAppDelegate

I added

To the code immediately after the array is created, can see the random possession if I send the array to NSLog, but there’s no item in the list in the application.

I added possesions to the ItemsViewController class, made it a property and synthesized it so I’d expect that to work. I’ll keep digging.

Thanks


#5

never mind, just found the typo in my code. oops.

( I am curious where the compiler is finding the ‘possesions’ symbol though.)


#6

Hi,

I guess possessions was nil so addObject just silently did nothing

What was the typo ?

Gareth


#7

I’d named the NSMutableArray “possesions” in the header and then declared “possessions” as a property and synthesized it.


#8

I see

So there were two different pointers defined: posessions and possessions

With the modern runtime - @property adds the instance variable automatically if it isn’t already defined.

So in effect your header looked like this:

[code]@interface ItemsViewController : UITableViewController {
ItemDetailViewController *detailViewController;
NSMutableArray *possessions;
NSMutableArray *possesions;
}

@property (nonatomic, retain) NSMutableArray *possessions;

@end
[/code]

In the app delegate we set one of these to an initialised object but the other was just a null pointer - all the calls to a null object will silently do nothing so the random possession wasn’t added to the array and therefore the numberOfRows didn’t tally with the tableView.

As far as the compiler is concerned though possesions and possessions are both valid symbols.

HTH
Gareth


#9

I am getting a similar crash - Assertion failure in -[UITableView _endCellAnimationsWithContext:], *** 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 (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (7 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
but my “numberOfRowsInSection” function is returning 7 always.

This crash is happening when I call “insertRowsAtIndexPaths”.