Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView adding/deleting sections while not in editing mode?
    text
    copied!<p>I have a <code>UITableView</code> and basically I'm making some in app settings, and if the first section's <code>UISegmentedControl</code> is toggled to index 1, then I want to display a new section, but if index 1 was previously set and the user selects index 0 then I need to remove section 2.</p> <p>To do this I had this code set to fire on the <code>UISegmentedControl's valueChanged event</code></p> <pre><code> if (segmentControl.selectedSegmentIndex == 0) { self.settings.useMetric = YES; if ([sections containsObject:FT_AND_IN] &amp;&amp; [sections containsObject:FRACTION_PRECISION]) { NSArray *indexSections = [NSArray arrayWithObjects: [NSIndexPath indexPathForRow:0 inSection: [sections indexOfObject:FT_AND_IN]], [NSIndexPath indexPathForRow:0 inSection: [sections indexOfObject:FRACTION_PRECISION]], nil]; [sections removeObject:FT_AND_IN]; [sections removeObject:FRACTION_PRECISION]; [self.tableView deleteRowsAtIndexPaths:indexSections withRowAnimation:UITableViewRowAnimationRight]; } } else { self.settings.useMetric = NO; [sections insertObject:FT_AND_IN atIndex:1]; [sections insertObject:FRACTION_PRECISION atIndex:2]; NSArray *indexSections = [NSArray arrayWithObjects: [NSIndexPath indexPathForRow:0 inSection: [sections indexOfObject:FT_AND_IN]], [NSIndexPath indexPathForRow:0 inSection: [sections indexOfObject:FRACTION_PRECISION]], nil]; [self.tableView insertRowsAtIndexPaths:indexSections withRowAnimation:UITableViewRowAnimationRight]; } </code></pre> <p>Where the <code>NSMutableArray</code> called <code>sections</code> is the list of all sections. Each section only has 1 row so no sub arrays are needed.</p> <p>However when evaluating the else part I get this error:</p> <pre><code>*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:904 *** 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 (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' </code></pre> <p>I've verified that it had 4 sections prior to the else, it correctly added the two sections to the <code>sections</code> array, I told it the proper indexPaths to the sections added. Why doesn't this work?</p> <p>I tried replacing the <code>[self.tableView insertRows/deleteRows...]</code> line with <code>[self.tableView reloadData];</code> and then it works fine, but I want to animate adding/deleting those sections.</p> <p><strong>Update</strong> I tried this suggestion and adding works but I'm getting crashing on removing</p> <pre><code>[self.tableView beginUpdates]; if (segmentControl.selectedSegmentIndex == 0) { self.settings.useMetric = YES; if ([sections containsObject:FT_AND_IN] &amp;&amp; [sections containsObject:FRACTION_PRECISION]) { [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: [sections indexOfObject:FT_AND_IN]] withRowAnimation:UITableViewRowAnimationRight]; [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: [sections indexOfObject:FRACTION_PRECISION]] withRowAnimation:UITableViewRowAnimationRight]; } } else { self.settings.useMetric = NO; [sections insertObject:FT_AND_IN atIndex:1]; [sections insertObject:FRACTION_PRECISION atIndex:2]; NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1]; NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndex:2]; [self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationRight]; [self.tableView insertSections:indexSet2 withRowAnimation:UITableViewRowAnimationRight]; } [self.tableView endUpdates]; </code></pre> <p>I get this error. </p> <pre><code>*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** - [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds maximum index value of NSNotFound - 1' </code></pre> <p>The <code>FT_AND_IN</code> and <code>FRACTION_PRECISION</code> objects are only added/removed from the data store in this code, and they are just <code>const NSString</code> objects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload