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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:39:00+00:00 2026-06-14T07:39:00+00:00

Scenario: I have an expense tracking iOS Application and I am storing expenses from

  • 0

Scenario:

I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity “Money” which is a parent entity for either an expense or an income.

My Question:

What I want is to basically categorize my expenses/incomes for a given week, a month, or year and display it as the section header title for example : (Oct 1- Oct 7, 2012) and it shows expenses/incomes amount and related stuff according to that particular week. Two buttons are provided in that view, if I would press the right button, it will increment the week by a week (Oct 1- Oct 7, 2012 now shows Oct8 – Oct 15, 2012) and similarly the left button would decrement the week by a week. I have two segment controls in the view as well. What I want to do is press the segment control which says “weekly” and if I press another segment which says “category” – how would I filter out my expenses/incomes per week based on category ?

I want to display two sections in my table view (one which shows the date for expenses and another to show the date for incomes in format (Oct1,2012 – Oct7, 2012)). How would I achieve this? I have written some pseudo code and if anybody can tell me how would I accomplish the above, that would be great.

EDIT – FETCH CONTROLLER

- (void)userDidSelectStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate
{    
    AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext * context = [applicationDelegate managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Money" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    [NSFetchedResultsController deleteCacheWithName:nil];

    //Here you create the predicate that filters the results to only show the ones with the selected date
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];
    [fetchRequest setPredicate:predicate];

    // Set the batch size to a suitable number.
     [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.

    NSSortDescriptor * sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];

    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];

    NSSortDescriptor *sortDescriptor3 = [[NSSortDescriptor alloc] initWithKey:@"cat" ascending:YES];

    NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, sortDescriptor3, nil];

    [fetchRequest setSortDescriptors:descriptors];
    [fetchRequest setIncludesSubentities:YES];
    [fetchRequest setResultType:NSManagedObjectResultType];

    if(_fetchedResultsController)
    {
          [_fetchedResultsController release]; _fetchedResultsController = nil;

    }
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"type" cacheName:nil];

    _fetchedResultsController.delegate = self;

    NSError *anyError = nil;

    if(![_fetchedResultsController performFetch:&anyError])
    {
        NSLog(@"error fetching:%@", anyError);
    }

    [sortDescriptor1 release];
    [sortDescriptor2 release];
    [sortDescriptor3 release];

    [fetchRequest release];

     //Finally you tell the tableView to reload it's data, it will then ask your NEW FRC for the new data
    [self.dashBoardTblView reloadData];

}

EDIT – Fetch Controller Delegate Methods

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
     [self.dashBoardTblView beginUpdates];

}


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

   switch(type) {
    case NSFetchedResultsChangeInsert:
        [self.dashBoardTblView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [self.dashBoardTblView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;
   }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{

    switch(type) {
    case NSFetchedResultsChangeInsert:

        [self.dashBoardTblView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:

        [self.dashBoardTblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeMove:

        [self.dashBoardTblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        [self.dashBoardTblView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                         withRowAnimation:UITableViewRowAnimationFade];


        break;

    case NSFetchedResultsChangeUpdate:
    {
        [self.dashBoardTblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
    }
  }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.dashBoardTblView endUpdates];
} 
  • 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-14T07:39:01+00:00Added an answer on June 14, 2026 at 7:39 am

    To group the “Expense”/”Income” objects into different sections, you need to add a type attribute to the Money entity, for example a Integer attribute that is 0 for expenses and 1 for incomes.

    To sort the table view sections by type (expense/income), use type as first sort descriptor:

    NSSortDescriptor *s1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
    

    To sort the entries within each section by date, use date as second sort descriptor:

    NSSortDescriptor *s2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
    
    NSArray *descriptors = [NSArray arrayWithObjects:s1, s2, nil];
    [fetchRequest setSortDescriptors:descriptors];
    

    Finally, to group the table view into sections by category, use type as sectionNameKeyPath:

    NSFetchedResultsController *aFetchedResultsController =
       [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                           managedObjectContext:context
                                             sectionNameKeyPath:type
                                                      cacheName:nil];
    

    This should give 2 sections, with all expenses in the first section and all incomes in the second section.

    Now for the section headers you have to implement tableView:viewForHeaderInSection: (I hope that I get this right):

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        Money *money = [[sectionInfo objects] objectAtIndex:0]; // The first object in this section
        NSNumber *type = money.type;
        if (type.intValue == 0) {
            // Create and return header view for "expense" section.
    
        } else {
            // Create and return header view for "incomes" section.
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have an expense tracking iOS Application and I am storing expenses from
Scenario: I have an expense tracking iOS Application and I am storing expenses from
Scenario : I have an expense tracking iOS Application and I am storing expenses
I have an expense tracking iOS Application using Core Data Model: Scenario: -> An
Scenario I have a 10 million row table. I partition it into 10 partitions,
Scenario: I have an application that pulls data from a SQL database as well
Scenario: I have an application that has a config table which stores the config
Scenario : I have a users table in my application. I also have two
Scenario: I have Window Application to be installed/updated from website. User needs to enter
Scenario: I have a console application that needs to access a network share with

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.