Grouped Table View, NSFetchedResultsController, Custom Rows

Discussion in 'iOS Programming' started by isthisonetaken, Feb 9, 2012.

  1. isthisonetaken macrumors regular

    Jun 29, 2006
    Hey All,

    I'm working on an iPad app that uses core data for storage. Users create objects (usually one a day) that contain many other objects themselves. The objects created daily each have a date (NSDate) and I have it right now that the table view only shows objects for a certain month (when the app launches, it's whatever month it currently is).

    I'd like to let the user see all the objects created in a grouped table view. What I want is the header section to be the year, and each row in a section to be a month, with the textLabel set to the month and the detailLabel set to the number of objects created within that month. I'm using an NSFetchedResultsController to grab all the objects with year (a separate attribute, unless there's another way to get the year from the NSDate object) as the sectionKeyPath and can split the grouped table into sections with the year as the header. What I can't figure out how to do is to get the sections to then only show the months that have objects, with a count, instead of all the different objects themselves.

    I tried using an NSMutableDictionary where the keys were the years and the objects were other nsmutabledictionary's were the keys were the months and the object stored was the count of objects. The problem I ran into was how to access them so that the years were in order and the months were in date order (as opposed to alphabetical) in the cellForRowAtIndexPath method...

    Any ideas?
  2. isthisonetaken thread starter macrumors regular

    Jun 29, 2006
    Ok, I have made some progress. I've got it right now so it has the years as sections, each month is a row and it's count of the objects is set as the detail text label. The problem I have right now is that the months are in alphabetical order, so April comes before February right now. I want it to be sorted by date.

    To get it working, I have an array representing the years (each section is an element in the array). Each element holds an NSMutableDictionary, where the key is the month name and it holds an NSNumber object representing the count.

    Any ideas of how to sort it by date instead of alphabetical?
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    NSDictionary is not meant to stored ordered objects, but keyed ones. You should consider somehow using an NSArray instead, which stores indexed objects.
  4. MattInOz macrumors 68030


    Jan 19, 2006
    I guess the question is How did the Month Objects get sorted that way?

    Couple of things come to mind about things to look at.
    First one falls into the the maybe I was really dumb category.
    With NSFetchedResults the sort key doesn't have to be anything that the user will see.

    So if you stored the say the first moment of the Month as a NSDate instead of the Name. Then sorting by that NSDate should put the months in the right order. With the added bonus that you can ask a NSDate for the Localised Month Name as a string to display. So that part will be taken care of you want to localise for other languages down the track.

    The other thing that comes to mind you might find useful to look at.
    With core data fetch requests once you have a fetchRequest configured you can ask it for the number of objects it will return. So instead of storing a count for the month if you can create a request based off your Month NSDate you could use that count for your Table. That way you don't have to worry about updating the counts yourself.

Share This Page