views:

95

answers:

1

I have a UITableView and basically I'm making some in app settings, and if the first section's UISegmentedControl 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.

To do this I had this code set to fire on the UISegmentedControl's valueChanged event

 if (segmentControl.selectedSegmentIndex == 0)
 {
     self.settings.useMetric = YES;
     if ([sections containsObject:FT_AND_IN] && [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];
 }

Where the NSMutableArray called sections is the list of all sections. Each section only has 1 row so no sub arrays are needed.

However when evaluating the else part I get this error:

*** 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).'

I've verified that it had 4 sections prior to the else, it correctly added the two sections to the sections array, I told it the proper indexPaths to the sections added. Why doesn't this work?

I tried replacing the [self.tableView insertRows/deleteRows...] line with [self.tableView reloadData]; and then it works fine, but I want to animate adding/deleting those sections.

Update I tried this suggestion and adding works but I'm getting crashing on removing

[self.tableView beginUpdates];
if (segmentControl.selectedSegmentIndex == 0)
{
        self.settings.useMetric = YES;
    if ([sections containsObject:FT_AND_IN] && 
            [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];

I get this error.

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -
    [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds 
    maximum index value of NSNotFound - 1'

The FT_AND_IN and FRACTION_PRECISION objects are only added/removed from the data store in this code, and they are just const NSString objects.

+1  A: 

It's very hard to read your unformatted code there.

You want to look at -[UITableView insertSections:withRowAnimation:] and -[UITableView deleteSections:withRowAnimation], I think.

Try:

if (segmentControl.selectedSegmentIndex == 0)
{
    self.settings.useMetric = YES;
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) {

        NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
        [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

        [sections removeObject:FT_AND_IN];
        [sections removeObject:FRACTION_PRECISION];

        [self.tableView deleteSections:indexSections
             withRowAnimation:UITableViewRowAnimationRight];
     }
 }
 else {
     self.settings.useMetric = NO;
     [sections insertObject:FT_AND_IN atIndex:1];
     [sections insertObject:FRACTION_PRECISION atIndex:2];

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

     [self.tableView insertSections:indexSections
          withRowAnimation:UITableViewRowAnimationRight];
}
Seamus Campbell
I'm still not sure why the edited version of myne didn't work, but yours does. The only problem with yours is the `[self.tableView addSections...]` line should be `insertSections`. Thanks!
jamone