user1107173
user1107173

Reputation: 10744

NSFetchedController: Displaying Sections out of order

I have three sections for an Events entity:

Upcoming  
Today    
Past     

Here is FRC setup:

- (NSFetchedResultsController *)fetchedResultsController
{
    if(_fetchedResultsController!=nil)
    {
        return  _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

     NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"modified"
                                                               ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:firstSort, nil];
     [fetchRequest setSortDescriptors:sortDescriptors];

    self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest
                                                                       managedObjectContext:self.managedObjectContext
                                                                         sectionNameKeyPath:@"sectionIdentifier"
                                                                                  cacheName:nil];
    self.fetchedResultsController.delegate = self;
    return self.fetchedResultsController;
}

Using a Transient property for the Entity, I setup the the sections as:

- (NSString *)sectionIdentifier
{    
    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveSectionIdentifier];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp)
    {
        NSDate *dateToCompare = [self getUTCFormateDate:[self modified]];
        NSCalendar* calendar = [NSCalendar currentCalendar];
        NSDate* now = [NSDate date];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        format.dateFormat = @"dd-MM-yyyy";
        NSString *stringDate = [format stringFromDate:now];
        NSDate *todaysDate = [format dateFromString:stringDate];

        NSUInteger differenceInDays =
        [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateToCompare] -
        [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:todaysDate];


        NSString *sectionString;

        switch (differenceInDays) {
            case -1:
                sectionString = @"Past";
                break;
            case 0:
                sectionString = @"Today";
                break;
            case 1:
                sectionString = @"Upcoming";
                break;
            }

        tmp = sectionString;
        [self setPrimitiveSectionIdentifier:tmp];
    }

    return tmp;
}

Currently the sections appear in descending order, where Upcoming is first, then Today, and then Past. I'd like to show Today's section first.

How can I display the sections in the following order?

Section 0: Today

Section 1: Upcoming

Section 2: Past

Upvotes: 0

Views: 129

Answers (1)

Wain
Wain

Reputation: 119031

The only thing controlling the order is the sort descriptor you have:

 NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"modified"
                                                           ascending:NO];

So you need to change that. You can make it ascending, but you describe a sort order which isn't directly linked to the date.

Because what you describe is dependent upon the current date you will need to make some modifications so that you have order information stored persistently into the model and which you update each day. You can then add this as your first your sort descriptor to use that, and keep the existing sort descriptor for ordering inside each section.

The information you need to store is as per the comment from @Volker, with a simple index number for each section.

Upvotes: 1

Related Questions