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 8784731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:10:29+00:00 2026-06-13T21:10:29+00:00

There is something within table sections and sortDescriptors that is driving me crazy. Any

  • 0

There is something within table sections and sortDescriptors that is driving me crazy. Any help will be much appreciated 😉

What I’m looking for is just displaying an “add” row at the bottom of each self.fetchedResultsController.fetchedObjects section. I’m using a sectionNameKeyPath to show sections in my table view. Up to here everything works perfectly. I’ve implemented this technique in other parts of my app and works pretty well.

However, the problem comes up when there is no data returned by the FRC and core data doesn’t return any entity. If I try to add my first managed object, then app breaks with the following error:

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 (1), plus or minus the number of sections
inserted or deleted (1 inserted, 0 deleted). with userInfo (null)

This is my code so far:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([self.fetchedResultsController.fetchedObjects count] == 0) {
        return 1;
    }
   return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    if ([self.fetchedResultsController.fetchedObjects count] == 0) {
        return 1;
    }
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    return [sectionInfo numberOfObjects] + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self.fetchedResultsController.fetchedObjects count] == 0 ) {

        static NSString *identificadorCelda2 = @"CeldaNuevoGasto";
        UITableViewCell *celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2];
        if (celda == nil) {
            celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2];
        }
        return celda;
    }

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:indexPath.section];

    NSInteger nsection = [sectionInfo numberOfObjects];

    if (indexPath.row == nsection) {

        static NSString *identificadorCelda2 = @"CeldaNuevoGasto";
        UITableViewCell *celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2];
        if (celda == nil) {
            celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2];
        }        
        return celda;
    }

    static NSString *CellIdentifier = @"CeldaGasto";

    CeldaGastos *cell = (CeldaGastos *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = (CeldaGastos *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    } .....

For sure I’m doing something wrong, but I can not figure it iut. Do I need to modify NSFetchedResultsController delegate methods as well to respond to additions? Or is it something easiser. Thanks 😉

Update

    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{

    NSInteger nsection = [self.fetchedResultsController.sections count];
    switch(type) {
        case NSFetchedResultsChangeInsert:
            if (nsection == 1 && [sectionInfo numberOfObjects] == 1) {
                    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

            }
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:

            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

            if (!nsection)
                [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
  • 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-06-13T21:10:31+00:00Added an answer on June 13, 2026 at 9:10 pm

    If I understand your situation correctly, then you indeed will have to change the NSFetchedResultsController delegate methods as well.

    Are you inserting new sections into table view using the -insertSections:withRowAnimation: method of your table view from some of the NSFetchedResultsController’s delegate callbacks?

    I do not know exactly what your NSFetchedResultsControllerDelegate’s implementation is, but for example consider this situation step by step:

    1. Initially you have no entities fetched, and your -numberOfSectionsInTableView: method returns 1 for your ‘add’ row.

    2. After you’ve inserted an entity and the NSFetchedResultsController’s delegate receives its -controller:didChangeSection:atIndex:forChangeType: callback, where you insert a new section into table view using -insertSections:withRowAnimation:

    3. The table view asks its dataSource by calling the -numberOfSectionsInTableView: method expecting it to rerurn a value increased by 1 (since you are inserting 1 new section). But the method does still return 1 because the ‘add’ row is not taken into account anymore.

    That will result exactly in the error which you are having.

    I guess calling -deleteSections:withRowAnimation: to remove the ‘add’ row when it is not needed anymore will solve your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to enclose some Oracle components within a Bash script that will
Is there something in EF 4.1 that allows for a reconnect to a database
Is there something in the Android developer guidelines that disuades developers from providing the
So I have a category table set up. Within this table there is a
Is there any way to pass a variable defined within R to the sqlQuery
I have a SQL table and within this table there is a BIT column
Is there something new about row-level security in SQL Server 2012? In 2008 and
Is there something equivalent to OmniCppComplete for java in vim ? I know of
Is there something akin to the .NET xsd.exe for java - generating classes from
Is there something like serialize/unserialize PHP functions in jQuery? These functions return a string

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.