{"title":"Custom Sections Title Selectors","html_url":"https://deallocatedobjects.com/posts/custom-sections-title-selectors","source_url":"","markdown_content":"Recently, I've been struggling with `NSFetchedResultsController`. I was trying to get it to section my objects by a \"pretty date\", but still have them sorted properly.\r\n\r\nThanks to a great post on [stackoverflow](http://stackoverflow.com/questions/1384345/using-custom-sections-with-nsfetchedresultscontroller/), I realized the beauty and simplicity of just adding an Objective-C Category to the `NSDate` property on my model object.\r\n\r\nMy managed object had a property called `scanTimestamp`, which was an `NSDate`. When configuring the `NSFetchedResultsController`, I was able to simply use `@\"scanTimestamp.prettySectionHeaderDateSring\"` as the `sectionKeyPath`.\r\n\r\n``` objc\r\nNSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];\r\n\r\nNSEntityDescription *entity = [NSEntityDescription entityForName:@\"PKScan\" inManagedObjectContext:self.managedObjectContext];\r\n[fetchRequest setEntity:entity];\r\n\r\nNSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@\"scanTimestamp\" ascending:NO];\r\nNSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];\r\n\r\n[fetchRequest setSortDescriptors:sortDescriptors];\r\n\r\n[fetchRequest setFetchBatchSize:20];\r\n\r\nNSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest \r\n                                                                                            managedObjectContext:self.managedObjectContext \r\n                                                                                              sectionNameKeyPath:@\"scanTimestamp.prettySectionHeaderDateSring\" \r\n                                                                                                       cacheName:nil];\r\n```\r\n\r\nThen, I just set `scanTimestamp` to be the `sortDescriptor` for the `NSFetchRequest` of the `NSFetchedResultsController`, and everything worked both smoothly, and correctly.\r\n\r\nIn the Category, the `- (NSString *) prettySectionHeaderDateSring` method was declared as returning an `NSString` and also as a readonly property (either of which would have been sufficient, but it's always nice to have both explicitly declared). Then I simply implemented the `prettySectionHeaderDateSring` method using some `NSDateFormatter` magic to transform the ugly `\"2010-10-13 10:33:12 -0400\"` NSDate value into a nice and pretty `\"Wednesday, October 13th\"` string.\r\n\r\nHere's the NSDate category:\r\n\r\n``` objc\r\n#import <Foundation/Foundation.h>\r\n\r\n@interface NSDate (PrettySectionHeaderAdditions)\r\n\r\n- (NSString *) prettySectionHeaderDateSring;\r\n\r\n@end\r\n\r\n@implementation NSDate (PrettySectionHeaderAdditions)\r\n\r\n- (NSString *) prettySectionHeaderDateSring {\r\n\tNSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];\r\n\r\n\t[formatter setDateFormat:@\"EEEE, LLLL d\"]; //Resolves to: \"Wednesday, September 13\"\r\n\t[formatter setAMSymbol:@\"am\"];\r\n\t[formatter setPMSymbol:@\"pm\"];\r\n\r\n\treturn [formatter stringFromDate:self];\r\n}\r\n\r\n@end\r\n```","html_content":"<p>Recently, I’ve been struggling with <code>NSFetchedResultsController</code>. I was trying to get it to section my objects by a “pretty date”, but still have them sorted properly.</p>\n\n<p>Thanks to a great post on <a href=\"http://stackoverflow.com/questions/1384345/using-custom-sections-with-nsfetchedresultscontroller/\">stackoverflow</a>, I realized the beauty and simplicity of just adding an Objective-C Category to the <code>NSDate</code> property on my model object.</p>\n\n<p>My managed object had a property called <code>scanTimestamp</code>, which was an <code>NSDate</code>. When configuring the <code>NSFetchedResultsController</code>, I was able to simply use <code>@&quot;scanTimestamp.prettySectionHeaderDateSring&quot;</code> as the <code>sectionKeyPath</code>.</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">NSFetchRequest </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">fetchRequest </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [[NSFetchRequest </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">alloc</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">init</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">NSEntityDescription </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">entity </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [NSEntityDescription </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">entityForName:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"PKScan\"</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> inManagedObjectContext:self</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">.managedObjectContext];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[fetchRequest </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setEntity:</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">entity];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSSortDescriptor</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> *</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">sortDescriptor </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [[</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSSortDescriptor</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> alloc</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">initWithKey:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"scanTimestamp\"</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> ascending:NO</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSArray</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> *</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">sortDescriptors </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [[</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSArray</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> alloc</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">initWithObjects:</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">sortDescriptor, </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">nil</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[fetchRequest </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setSortDescriptors:</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">sortDescriptors];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[fetchRequest </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setFetchBatchSize:20</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">NSFetchedResultsController </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">aFetchedResultsController </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [[NSFetchedResultsController </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">alloc</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">initWithFetchRequest:</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">fetchRequest </span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">                                                                                            managedObjectContext:self</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">.managedObjectContext </span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">                                                                                              sectionNameKeyPath:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"scanTimestamp.prettySectionHeaderDateSring\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> </span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">                                                                                                       cacheName:nil</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span></code></pre></figure>\n<p>Then, I just set <code>scanTimestamp</code> to be the <code>sortDescriptor</code> for the <code>NSFetchRequest</code> of the <code>NSFetchedResultsController</code>, and everything worked both smoothly, and correctly.</p>\n\n<p>In the Category, the <code>- (NSString *) prettySectionHeaderDateSring</code> method was declared as returning an <code>NSString</code> and also as a readonly property (either of which would have been sufficient, but it’s always nice to have both explicitly declared). Then I simply implemented the <code>prettySectionHeaderDateSring</code> method using some <code>NSDateFormatter</code> magic to transform the ugly <code>&quot;2010-10-13 10:33:12 -0400&quot;</code> NSDate value into a nice and pretty <code>&quot;Wednesday, October 13th&quot;</code> string.</p>\n\n<p>Here’s the NSDate category:</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">#import</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> &#x3C;Foundation/Foundation.h></span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">@interface</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> NSDate</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> (PrettySectionHeaderAdditions)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">- (</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSString</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> *</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">prettySectionHeaderDateSring</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">@end</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">@implementation</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> NSDate</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> (PrettySectionHeaderAdditions)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">- (</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSString</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> *</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">prettySectionHeaderDateSring</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">    NSDateFormatter</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> formatter </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [[[</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">NSDateFormatter</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\"> alloc</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">init</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">autorelease</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    [formatter </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setDateFormat:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"EEEE, LLLL d\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">]; </span><span style=\"--shiki-light:#6A737D;--shiki-dark:#6A737D\">//Resolves to: \"Wednesday, September 13\"</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    [formatter </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setAMSymbol:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"am\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    [formatter </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setPMSymbol:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"pm\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [formatter </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">stringFromDate:self</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">@end</span></span></code></pre></figure>","created_at":"2013-11-16T05:10:25.696Z","updated_at":"2013-11-16T05:10:25.696Z"}