iPhone: Group by month - Core Data

前端 未结 2 2012
野性不改
野性不改 2021-01-14 10:22

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 da

相关标签:
2条回答
  • 2021-01-14 10:55

    Apple has a really great sample showing exactly how you can achieve this. I think it is what you are looking for.

    Check out: http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009939

    0 讨论(0)
  • 2021-01-14 11:08

    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.

    0 讨论(0)
提交回复
热议问题