tags:

views:

78

answers:

2

Hi

I store dates in a core data entity attribute, I then display the date in the table cells. I want to group all the cells according to months and only display the relevant dates in the correct sections.

I'm using sectionNameKeyPath of the NSFetchedResultsController, but I need to only use the month in the date to group. How can I achieve this? Would I need to create a new attribute and store just the month as a string or can I use NSPredicate?

Please can anyone let me know if they have done this or give me some examples.

Thanks

A: 

Hi Shaun,

Have you figured out how to achieve this?

avi
A: 

Step one, make sure you have a Managed Object Class in your project that represents the core data entity that stores the date. Step two, in the implementation file for this entity, write a method that returns the month for that object. It should look at the date variable and use NSDateComponents methods to determine the month. You need not create a new attribute for it. Step three, when you initialize your NSFetchedResultsController, pass the name of the method that returns the month as the sectionNameKeyPath parameter. What you are essentially doing is creating a "getMonth" method that programmatically determines the month, and giving the value returned by that to the sectionNameKeyPath. I hope this is clear. Please ask for clarification if not.

Note. Make sure your month method returns the month in a way that sorts in the proper order (ie not alphabetically), and that your Table View translates that into a meaningful string.

Drew C