views:

110

answers:

1

Hi

I have a app that works with Core Data. The data has a field with a date and I would like to show every entry of a month in a seperate section.

How do I actually get the data ? I use a NSFetchedResultsController to get the data and use this :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{

    id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];

}

to get the rows and this :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
MyInfoObject *info = [_fetchedResultsController objectAtIndexPath:indexPath];
}

to get my actually data object.

Thanks

+1  A: 

Make sure you set the sectionNameKeyPath when instantiating your fetchedResultsController

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"dateKey" cacheName:@"Root"];
Travis
Unfortunately this gives me a section for every date. What I want is just the month, any idea I can accomplish that ?
eemceebee
I would add another attribute, probably call it month_year ... and reference the document here ... http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/Articles/cdAccessorMethods.html#//apple_ref/doc/uid/TP40002154-SW13
Travis
Create a custom managed object class, which is what I usually do ... and create a custom getter and setter similar to the way its done in Listing 2 and Listing 3. In the setter I would transform the date object into a string "MMYYYY", and in the getter I would check for the value, and if it doesn't exist, try return the same format of date that you used in the getter.
Travis
After you implement those getters and setters and they work, change the sectionNameKeyPath:@"dateKey" to your new month_year key.
Travis