IOS app crashes when deleting last row from tableview section

I study editable partitioned UITableViews, and it’s hard for me to understand why my application crashes when deleting the last row in one of the sections. I searched here with Google, and it seems to me that I have already done what most people say, fixes this problem, but no luck. I am confused and grateful for any help.

here is my method to delete table rows

myTableSections is an NSMutableArray containing NSDictionaries that represent sections - "Holds" is the key that contains the array for the strings, and the "Head" key for the section heading

PossessionStore is a singleton that contains all the accessories for the application in the allPossessions array.

msg for partitioning is a method of sorting allPossessions into NSDictionaries and storing them in 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, yes, I almost forgot this last part - debugging information - at this moment there are two lines in section 1 and one line in section 2. Then I delete one line from section 1 and then the last line from section 1. according to NSLog it says that I am inserting, and the msg error is the number of sections is correct, as it should be at this point, but does not show the section that is ever deleted. 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) 0x3409 0xb26e2 0x208e6b 0x13cde99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x12b6ef5 0x13a0195 0x1304ff2 0x13038da 0x1302d84 0x1302c9b 0x12b57d8 0x12b588a 0x16626 0x20dc 0x2085) 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) 
+4
source share
2 answers

I apologize for everyone pointing me in the right direction last night. I was too tired and disappointed to get it; I banged my head about it for too long. Here is my final decision that fixed it.

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { ////If the table view is asking to commit the delete command if (editingStyle == UITableViewCellEditingStyleDelete) { BOOL lastRow = FALSE; if ([tableView numberOfRowsInSection:[indexPath section]] == 1 ) { lastRow = TRUE; } // 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 or section if last row from the table view with an animation if (lastRow) { // NSLog(@"Should be deleting section"); [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:YES]; } else { // NSLog(@"Should be deleting row"); [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; } } } 
+8
source

All sections of a UITableview must have at least one row.

Therefore, if you want to delete the last line, you need to delete the section:

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

Make sure your data handles empty partitions correctly (when providing the number of partitions, etc.).

+8
source

Source: https://habr.com/ru/post/1400933/


All Articles