views:

11

answers:

2

Hi guys, i just started two weeks ago with ObjectiveC and i'm a complete noob so, go easy on me. I want to load a tableView with the following:

Upcoming Events
.event4 xx/xx/2010
.event5 xx/xx/2010
Pass Events
.event1 xx/xx/2008
.event4 xx/xx/2008
.event5 xx/xx/2008

i'm using Core Data with a fetchedResultsCOntroller. What's the best way to accomplish this?

Best Regards
Ricardo Castro

+1  A: 

Set the sortDescriptors on the NSFetchRequest that you give to the NSFetchedResultsController. If you have a field that's called date on your NSManagedObject, you can make a sort descriptor on that key.

For separating into sections, you'll want to add a method on your object that either returns "Upcoming Events" or "Past Events" and then use the name of that method as the sectionNameKeyPath for the NSFetchedResultsController.

It's important that the sort that you put in the sort descriptors matches up with how the objects are split into sections. ie. You don't want any past event being sorted before an upcoming event, but for your case that shouldn't be too hard. :)

Andrew Pouliot
hks Andrew! it was easier than i thought.
ricastro
A: 

thks Andrew! it was easier than i thought. here's the solution.

@implementation NSManagedObject (dateSection)
- (NSString *)sectionNameGen
{
    [self willAccessValueForKey:@"sectionNameGen"];
    NSString *sectionName =[[[NSString alloc]init]autorelease];


    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[self valueForKey:@"date"]];
    NSDate *tempDate = [cal dateFromComponents:components];


        if([today isEqualToDate:tempDate])
        {
            sectionName = @"Today's Events";
        }
        else
        {
            if ([today timeIntervalSinceDate:tempDate]<0) 
            {
                sectionName = @"Upcoming Events";
            }
            else 
            {
                sectionName=@"Passed Events";
            }   
        }   

    [self didAccessValueForKey:@"sectionNameGen"];
    return sectionName;
}
@end

and i used sectionNameGen in the sectionNameKeyPath

ricastro