3
votes

I need your help :(

I'm working on a iOS application in which i had to remove some rows and sections in a UItableView.

I'm on Xcode 4.

I associate my tableView with NSarray and dictionary, like this :

NSMutableArray *arrTemp1 = [[NSMutableArray alloc]
                         initWithObjects:@"Chris",nil];

    NSMutableArray *arrTemp2 = [[NSMutableArray alloc]
                         initWithObjects:@"Bianca",nil];

    NSMutableArray *arrTemp3 = [[NSMutableArray alloc]
                         initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableArray *arrTemp4 = [[NSMutableArray alloc]
                                initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableArray *arrTemp5 = [[NSMutableArray alloc]
                                initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableArray *arrTemp6 = [[NSMutableArray alloc]
                                initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableDictionary *temp = [[NSMutableDictionary alloc]
                         initWithObjectsAndKeys:arrTemp1,@"A",arrTemp2,
                                 @"B",arrTemp3,@"C",arrTemp4, @"D", arrTemp5, @"E", arrTemp6, @"J",nil];
    self.tableContents = temp;
    self.sortedKeys =[[self.tableContents allKeys]
                      sortedArrayUsingSelector:@selector(compare:)];

When i need to delete a rows or a sectionm i'm using this code ->

(void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {   
        NSMutableArray *listData = [self.tableContents objectForKey:
                            [self.sortedKeys objectAtIndex:[indexPath section]]];
        NSUInteger row = [indexPath row];

        [listData removeObjectAtIndex:row];

        [tableView beginUpdates];


        if ([listData count] > 0)
        {
            // Section is not yet empty, so delete only the current row.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }
        else
        {
            // Section is now completely empty, so delete the entire section.
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                     withRowAnimation:UITableViewRowAnimationFade];
        }

       [tableView endUpdates];
    }
}

When i'm deleting rows, it works well.. When i'm deleting sections, i'm getting the following error : " * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1912.3/UITableView.m:1030 2012-01-13 16:42:45.261 * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (6) must be equal to the number of sections contained in the table view before the update (6), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).'"

 (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [tableContents count]; // tableContent is a NSMUtableDictionnary*
}

- (NSInteger)tableView:(UITableView *)table
 numberOfRowsInSection:(NSInteger)section 
{
    NSArray *listData =[self.tableContents objectForKey:
                        [self.sortedKeys objectAtIndex:section]];
    return [listData count];
}

I'm getting crazy, and coming on stackOverflow to ask for help...

(Sorry for my poor english :( )

Thanks to everyone reading and answering !!

2
Maybe you need to remove the row before removing the section ? Wild guess.ksol
I've already try it. And the error is the same that before, but this time, it says that the number of rows changedUIChris

2 Answers

2
votes

It looks like you're not actually deleting the dictionary key for the empty array when it contains no objects, thus your call to

        // Section is now completely empty, so delete the entire section.
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                 withRowAnimation:UITableViewRowAnimationFade];

is causing the problem as you still have the same number of sections (dictionary keys) as before...

1
votes

Try swapping these 2 lines around:

[listData removeObjectAtIndex:row];

[tableView beginUpdates];

So it should be:

[tableView beginUpdates];

[listData removeObjectAtIndex:row];