Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:16:04+00:00 2026-05-16T06:16:04+00:00

I have a UITableView and basically I’m making some in app settings, and if

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:16:05+00:00Added an answer on May 16, 2026 at 6:16 am

    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];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.