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?