Separating UITableView data into sections from one source
I have one table in my core data source which holds some articles with NSDate's. I basically want to separate the NSManagedObject into days, so you can scroll through and separate articles by date.
What is the best way to approach this? My context is queried out in descending date order, so just need to split that up into days for the UITableView sections, rather than 1 big section.
When you load the data for your table -- whether that's using NSFetchedResultsController
and Core Data, or loading a .plist file into an NSDictionary
object -- you can "section" it as you like.
You'll first step through the data (eg use the fetchedObjects
property if using NSFetchedResultsController
) and determine what sections you want. Since you're wanting to split on dates, you might store cutoffs represented by NSDate
objetcts in an array. You would then use this array to implement the various UITableViewDataSource
methods: in numberOfSectionsInTableView:
you can return the count
of this array, and in sectionIndexTitlesForTableView:
you can return NSString
representations of those stored NSDate
s for the section titles.
Methods like tableView:numberOfRowsInSection:
and tableView:cellForRowAtIndexPath:
are a little trickier. You'll probably want to use another "preprocessed" data structure (perhaps a two-level array of arrays, where the inner arrays contain the entity objects you fetched), and you'll want to set up as the same time as your array of NSDate
objects.