I have a UITableView that I never want to fall below 1 cell: It's a directory readout, and if there's no files in the directory, it has a single cell that says "No Files". (In edit mode, there's a bonus cell to Create a File, so edit mode never falls below two cells.)
It's probably just lack of sleep keeping me from thinking my way out of a paper bag right now, but I keep tripping up on errors like this:
*** 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 (2) 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 (1 inserted, 0 deleted).'
This is happening as I'm preemptively adding the "No Files" placeholder before deleting the last file. There's a like crash if I delete the last file cell before adding the placeholder. Either way, the cell count gets out of sync with the return from numberOfRowsInSection, and that triggers a crash.
Surely there's a design pattern for this situation. Clue me in?
Do something along the lines shown in the code snippet below:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // First delete the row from the data source [self deleteTableData: indexPath.row]; // method that deletes data from if ([self.tableDataArray count] != 0) { [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else { [self.tableView reloadData]; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With