ylliX - Online Advertising Network

Dealing with NSInternalInconsistencyException when removing UITable row

Stupid situation, today I wanted to test deleteRowsAtIndexPaths method from UITable withRowAnimation:UITableViewRowAnimationFade – just to check how it will look, did not wanted to remove my db record and add it every run, but always I was getting:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  
The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), 
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved 
into or out of that section (0 moved in, 0 moved out).'

The reason was simple – in my array I still had removed entry and it caused such exception. So if you want to test something and don’t want to remove db record, you need to remove it at least from array where UITable data is stored, in my case:

[myArray removeObjectAtIndex:myRow];

just helped, and in case anyway will look for a way to remove table row while having only its index:

NSIndexPath * path= [NSIndexPath indexPathForRow:myRow inSection:0];
[tblSeries deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationFade];

And thats all, you don’t need (or even you cannot) use reloadData method. Of course if your table has sections, you need to put proper value as “inSection” argument. The simples and most elegant way is to extend UITableViewCell and add your properties for holding row and section:

@interface MyCell : UITableViewCell

@property int row;
@property int section;

@end

 

Leave a Reply