Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS app crashes when deleting the last row from a tableview section

I am learning editable sectioned UITableViews and I am having trouble figuring out why my app crashes when deleting the last row in one of the sections. I've been looking around here and google and it looks to me that I have already done what most people say fixes this problem but no luck. I am stumped and appreciative of any help offered.

here is my method for deleting the table rows

myTableSections is an NSMutableArray holding NSDictionaries that represent the sections - "Possessions" is the key holding an array for the rows, and a key "Header" for the section header

PossessionStore is a singleton that holds all Possessions for the app in allPossessions array

The msg to divideSections is a method to sort allPossessions into NSDictionaries and store those into myTableSections

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{       ////If the table view is asking to commit the delete command
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Get the possesion from the row
        Possession *p = [[[myTableSections objectAtIndex:[indexPath section]] objectForKey:@"Possessions"] objectAtIndex:[indexPath row]];

        // delete it from the PossessionStore
        [[[PossessionStore defaultStore] allPossessions] removeObjectIdenticalTo:p];

        // Rebuild myTableViewSections to reflect the data change to the PossessionStore
        [self divideSections];

            // remove the row from the table view with an animation
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }
}

Oh yeah almost forgot this last part - debug info - at this point there are two rows in section 1 and one row in section 2. I then delete one row from section 1 and then the final row from section 1. according to the NSLog reports i put in and the error msg the section count is correct as it should be at that point but it doesn't show the section ever being removed. If I understand it correctly??????????

2012-03-12 00:40:49.938 HomePwnr[53434:f803] commitEditingStyle:forRowAtIndexPath: called
2012-03-12 00:40:49.940 HomePwnr[53434:f803] myTableSections is holding - (
        {
        Header = "Cheap Stuff";
        Possessions =         (
            "Red Car (2U8F9): Worth $20, recorded on 2012-03-12 04:40:38 +0000",
            "Blue Gun (0X2Q7): Worth $14, recorded on 2012-03-12 04:40:40 +0000"
        );
    },
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:40:49.942 HomePwnr[53434:f803] divideSections called
2012-03-12 00:40:49.944 HomePwnr[53434:f803]  End of divideSections myTableSections is holding - (
        {
        Header = "Cheap Stuff";
        Possessions =         (
            "Red Car (2U8F9): Worth $20, recorded on 2012-03-12 04:40:38 +0000"
        );
    },
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:40:49.946 HomePwnr[53434:f803] returning number of sections: 2
2012-03-12 00:40:49.947 HomePwnr[53434:f803] returning number of sections: 2
2012-03-12 00:40:49.949 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:40:49.951 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:40:49.953 HomePwnr[53434:f803] tableView:numberOfRowsInSection called for section 0, returning - 1
2012-03-12 00:40:49.954 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 1
2012-03-12 00:40:49.956 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 1
2012-03-12 00:40:49.957 HomePwnr[53434:f803] tableView:numberOfRowsInSection called for section 1, returning - 1
2012-03-12 00:41:32.232 HomePwnr[53434:f803] commitEditingStyle:forRowAtIndexPath: called
2012-03-12 00:41:32.234 HomePwnr[53434:f803] myTableSections is holding - (
        {
        Header = "Cheap Stuff";
        Possessions =         (
            "Red Car (2U8F9): Worth $20, recorded on 2012-03-12 04:40:38 +0000"
        );
    },
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:41:32.236 HomePwnr[53434:f803] divideSections called
2012-03-12 00:41:32.238 HomePwnr[53434:f803]  End of divideSections myTableSections is holding - (
        {
        Header = "Expensive Stuff";
        Possessions =         (
            "Blue Mac (7R3K0): Worth $97, recorded on 2012-03-12 04:40:36 +0000"
        );
    }
)
2012-03-12 00:41:32.239 HomePwnr[53434:f803] returning number of sections: 1
2012-03-12 00:41:32.241 HomePwnr[53434:f803] returning number of sections: 1
2012-03-12 00:41:32.242 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:41:32.244 HomePwnr[53434:f803] tableView:titleForHeaderInSectionCalled - section = 0
2012-03-12 00:41:32.246 HomePwnr[53434:f803] tableView:numberOfRowsInSection called for section 0, returning - 1
2012-03-12 00:41:32.248 HomePwnr[53434:f803] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1021
2012-03-12 00:41:32.250 HomePwnr[53434:f803] *** 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 (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
*** First throw call stack:
(0x13cc022 0x155dcd6 0x1374a48 0x9ad2cb 0x9b103 0xa66d2 0xa674d 0x3409 0xb26e2 0x208e6b 0x13cde99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x12b6ef5 0x13a0195 0x1304ff2 0x13038da 0x1302d84 0x1302c9b 0x12b57d8 0x12b588a 0x16626 0x20dc 0x2085)
terminate called throwing an exception(lldb) 
like image 305
vichudson1 Avatar asked Dec 02 '22 23:12

vichudson1


1 Answers

All UITableview sections must have at least one row.

Therefore, when you want to delete the last row, you have to delete the section instead:

 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

Make sure that your data properly handles empty sections (when providing the number of sections, etc.)

like image 60
lnafziger Avatar answered Jan 04 '23 23:01

lnafziger