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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:58:53+00:00 2026-05-29T10:58:53+00:00

I have a UITableView driven by a NSFetchedResultsController. I call a method to create

  • 0

I have a UITableView driven by a NSFetchedResultsController. I call a method to create a NSPredicate that filters my entities that have a timestamp since today. And in my NSFetchedResultsController I apply the predicate.

if (appDelegate.displayTodayOnly) {
    [fetchRequest setPredicate:datePredicate];
}

This all works great. However, occasionally I need to view the entries previous to ‘today.’ I do this by swiping down on the table view when it is already at the beginning of the table.

-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {  
    if (self.tableView.contentOffset.y<0) {
        if (appDelegate.displayTodayOnly) {
            [appDelegate setDisplayTodayOnly:NO];
            NSInteger resultsOffset = [[self.fetchedResultsController fetchedObjects] count];
            //[self.tableView beginUpdates];
            [__fetchedResultsController release];
            __fetchedResultsController=nil;
            NSError *error;
            if (![[self fetchedResultsController] performFetch:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                exit(-1);  // Fail
            }
            //[self.tableView endUpdates];
            NSInteger fullCount = [[self.fetchedResultsController fetchedObjects] count];
            if (fullCount>resultsOffset) {
                NSUInteger sect = [[self.fetchedResultsController sections] count]-2;
                id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:sect];
                NSUInteger rows = [sectionInfo numberOfObjects]-1;
                NSIndexPath *indexing = [NSIndexPath indexPathForRow:rows inSection:sect];
                NSLog(@"IndexPath = section:%d row:%d",sect,rows);
                [self.tableView reloadData];
                [self performSelector:@selector(scrollToIndexPath:) withObject:indexing afterDelay:0.0001];
            }
        }
    }
}

This works also, but is inelegant. It discards the fetchedResultsController, and reloads the table starting with the entries from the beginning of time as far as the entries are concerned. Then, I have it scroll to the index path above the entry of the previous table.

What I would like to happen is for the entries to load and insert before the top of the current table. Does anyone know of an elegant way to change the fetch for that to happen?

  • 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-29T10:58:53+00:00Added an answer on May 29, 2026 at 10:58 am

    I’m posting my solution in hopes that it may help someone else in need.

    I’m still setting the NSFetchedResultsController instance to nil, and getting a new one with a different predicate. Then, instead of reloading the table (which caused the disorienting jump), in a for(loop) I manually call [self.tableView insertSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationNone]; which comes almost directly out of the boilerplate controllerWillChangeContent: of <NSFetchedResultsControllerDelegate> method. This works because the last section is always going to be the section for today. If there aren’t any entries for today, I need to reloadData because there will be an inconsistency of the number of sections that are inserted because today’s empty section counts as a section.

    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {   
        if (self.tableView.contentOffset.y<0) {
            if (appDelegate.displayTodayOnly) {
                [appDelegate setTodayOnlyMemory:NO];
                NSUInteger resultsOffset = [[self.fetchedResultsController fetchedObjects] count];
               [__fetchedResultsController release];
                __fetchedResultsController = nil;
                self.fetchedResultsController = nil;
    
                NSError *error = nil;
                if (![self.fetchedResultsController performFetch:&error])
                {
                    NSLog(@"error in fetched results controller of Memory Notes");
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                }
                if (resultsOffset) {
                    [self.tableView beginUpdates];
                    NSInteger afCount = ([[self.fetchedResultsController sections] count]-1);
                    for (NSInteger i=0; i<afCount; i++) {
                        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationNone]; 
                    }
                    [self.tableView endUpdates];
                } else {
                    [self.tableView reloadData];
                }            
                [self performSelector:@selector(scrollToSpot) withObject:nil afterDelay:0.01f];
            }
        }
    }
    

    Additionally, here is my scrollToSpot method for ending up at the right spot.

    -(void)scrollToSpot
    {    
        NSArray *sectArray = [self.fetchedResultsController sections];
        if ([sectArray count]>1) {
            id <NSFetchedResultsSectionInfo> sectionInfo = [sectArray objectAtIndex:([sectArray count]-2)];
            NSInteger theRow = [sectionInfo numberOfObjects]-1;
            NSIndexPath* ipath = [NSIndexPath indexPathForRow:theRow inSection:([sectArray count]-2)];
            NSLog(@"Row:%d",ipath.row);
            [self.tableView scrollToRowAtIndexPath:ipath atScrollPosition:UITableViewScrollPositionTop animated:NO];
            NSLog(@"ContentOffset:%.0f",self.tableView.contentOffset.y);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView cell that is going to have a variable size depending
I have a UITableView that displays single large images in each cell. The names
I have a uitableview that loads fairly large images in each cell and the
I have a UITableView that I have set-up with 2 sections. The code below
I have a UIViewController, and within that view i have UITableView added in IB
I have a UITableView that I populate with data that i download over the
I have an UITableView, and in it I have different sections - words that
I have a UITableView that has photos, i get these photos from URLs and
I have UITableView that will have several child UITableViews which may also have 1
I have a UITableView that has a disclosure button on every row. When the

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.