0

I wonder if anyone could help me. I am trying to group the date records by month or year only and not by day. I appreciate your help.

`(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {

 id theSection = [[self.fetchedResultsController sections] objectAtIndex:section];

/* Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month. To display the section title, convert the year and month components to a string representation. */

     static NSDateFormatter *formatter = nil;

     if (!formatter) { formatter = [[NSDateFormatter alloc] init];  [formatter setCalendar:[NSCalendar currentCalendar]];

    NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMM YYYY" options:0 locale:[NSLocale currentLocale]];
    [formatter setDateFormat:formatTemplate];
    }

     NSInteger numericSection = [[theSection name] integerValue];
    NSInteger year = numericSection / 10000;
    NSInteger month = (numericSection - (year * 10000))/100;
   NSInteger day = (numericSection - (year * 10000 + month * 100));


    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.year = year;
    dateComponents.month = month;
    dateComponents.day = day;
    NSDate *date = [[NSCalendar currentCalendar]           dateFromComponents:dateComponents];

    NSString *titleString = [formatter stringFromDate:date];

    return titleString;
    }`
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
elton
  • 3
  • 1

1 Answers1

0

You need to implement the custom section title as a property in you NSManagedObject subclass (in a category, if you autogenerate NSManagedObject classes). This stackoverflow answer is what you are looking for.

Community
  • 1
  • 1
Antonis
  • 114
  • 7
  • Thanks Antonis for replying. I tried your suggestion but it did not work for me. I am using NSFetchedResultsController to manage my core data records. Using the NSInteger date calculation above works fine on grouping records by day but I wanted to group them by month and year separately. Thanks again – elton Jul 29 '15 at 10:58