XCODE TableView with multiple sections and delegate datasource from array of arrays - can't delete row without error?

StackOverflow https://stackoverflow.com/questions/16675006

Question

I have a TableView with multiple sections that's got a delegate Data Source which links to an array of arrays. Each array within the array of arrays is it's own Table View Section. I have it loading up correctly and have an edit button enabled but when I try and delete my app crashes with the following error.

*** 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 (9) must be equal to the number of sections contained in the table view before the update (10), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

Now I am NOT deleting a section so am somewhat confused, here is a sample of my code. I have searched this forum extensively but just can't find out what I have done wrong.

HERE IS MY CODE:

// Calculate how many sections in the table view from the array or arrays

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{

    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];

    NSInteger sections = [delegate.packing.packingListArray count];

    return sections;
}

// Load the array and extract the count of items for each section

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

    // For each array within the array of array count the items
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSArray *sectionContents = [delegate.packing.packingListArray objectAtIndex:section];
    NSInteger rows = [sectionContents count];

    return rows;
}

// Load the list into the table view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Set the cell identifier - using ID field thats been set in interface builder
    static NSString *CellIdentifier = @"DestinationCell";

    // Re-use existing cell?
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // If no reusabled cells then create a new one
    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Add the relevant packing list array from the array of arrays
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSArray *sectionContents = [delegate.packing.packingListArray objectAtIndex:[indexPath section]];
    NSString *contentForThisRow = [sectionContents objectAtIndex:[indexPath row]];

    cell.textLabel.text = contentForThisRow;



    // Return the formatted cell with the text it needs to display in the row
    return cell;
}

// Delete row from table and update data source array

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

    if (editingStyle ==UITableViewCellEditingStyleDelete) {


        [tableView beginUpdates];

        // Delete the item from the array
        AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
        [delegate.packing.packingListArray  removeObjectAtIndex:indexPath.row];


        // Delete the row from the table view
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];

    }


}

I have a feeling this is something to do with perhaps the complication of the array of arrays creating multiple sections but there mus be someway of doing this?

Any help much appreciated as I've been looking at this same code now for hours.

Was it helpful?

Solution

I finally managed to resolve the issue, there were a couple of problems in then end. It was all due to the way I was trying to remove the item from the array or arrays. The code I should have used was this:

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
[[delegate.packing.packingListArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];

I then had an issue with the way I had my arrays defined as they were built from a plist which although set as NSMutableArray they were being converted to NSArray. So at the end of that statement I included.

........mutablecopy];

All problems resolved :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top