1

I only have this problems with iOS 5, no problems with iOS 6

This is my log

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 3.  The number of rows contained in an existing section after the update (3) 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 (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

And my code

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[dictionary allKeys] count];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *keyArray = [dictionary allKeys];
    return [[dictionary objectForKey:[keyArray objectAtIndex:section]] count];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
=    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        //First get all the keys of dictionary into one array
        NSArray *sectionsArray = [dictionary allKeys];
        //Get all the data of tapped section into one array by using indexpath.section
        NSMutableArray *objectsAtSection = [dictionary objectForKey:[sectionsArray objectAtIndex:indexPath.section]];
        //remove the particular object by using indexPath.row from the array
        [objectsAtSection removeObjectAtIndex:indexPath.row];
        // Update dictionary

        [table beginUpdates];

        // Either delete some rows within a section (leaving at least one) or the entire section.
        if ([objectsAtSection count] > 0)
        {
            [dictionary setObject:objectsAtSection forKey:[sectionsArray objectAtIndex:indexPath.section]];
            // Section is not yet empty, so delete only the current row.
            // Delete row using the cool literal version of [NSArray arrayWithObject:indexPath]
            [table deleteRowsAtIndexPaths:@[indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [dictionary removeObjectForKey:[sectionsArray objectAtIndex:indexPath.section]];
            // Section is now completely empty, so delete the entire section.
            [table deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                     withRowAnimation:UITableViewRowAnimationFade];
        }
        [table endUpdates];
    }
}

in iOS 5, after delete some row, and some section, i have this problems.

Could you please help me?

Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
Nam Vu
  • 5,669
  • 7
  • 58
  • 90
  • I think problem in [dictionary allKeys] i don't no in (ios 5) , allKeys returns random order of keys just check putting break points in these statements.(i am not sure about this just try ) – Shankar BS Jul 23 '13 at 10:40
  • but in iOS 5, after delete some row, and some section, i have this problems. – Nam Vu Jul 23 '13 at 10:51
  • Just add [self.tableView reloadData]; and self.editing = YES; This is needed because the table view doesn't initially have information about its data source and delegate; if you create a table view, it always needs to be sent a reloadData message as part of its initialization. – 9to5ios Jul 23 '13 at 10:52
  • use [dictionary count]; every where (not sure) – Shankar BS Jul 23 '13 at 11:08
  • Is your `dictionary` an NSMutableDictionary object? – Stas Jul 23 '13 at 11:11
  • yes, NSMutableDictionary – Nam Vu Jul 23 '13 at 15:40

2 Answers2

3

Need to add

[self.tableView reloadData];

and

self.editing = YES;

This is needed because the table view doesn't initially have information about its data source and delegate; if you create a table view, it always needs to be sent a reloadData message as part of its initialization.

Hope it will Help you

9to5ios
  • 5,319
  • 2
  • 37
  • 65
  • still have this problems after delete 10 sections – Nam Vu Jul 23 '13 at 11:00
  • Also suggesting you to check that your tableView property is properly connected to UITableView in your interface XIB file. Do NSLog(@"%@", self.tableView); in your viewWillAppear: also to get an updated list, it was necessary to call the viewdidLoad method again, in addition to the self.tableView reloaddata. Hope it will help you – 9to5ios Jul 23 '13 at 11:01
2

Ok, it seems I found an answer. See this SO question and answer, explaining why using allKeys method is bad.

As long as you don't add or remove any elements from the dictionary, they will remain in the same order, but as soon as you add or remove an element, the new order will be completely different.

Community
  • 1
  • 1
Stas
  • 641
  • 8
  • 16
  • - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [dictionary count]; } i try to use this, but nothing change – Nam Vu Jul 23 '13 at 15:54
  • replace NSMutableDictionary by OrderedDictionary is good way. You save my life. Thanks – Nam Vu Jul 23 '13 at 18:00