Sandeep
Sandeep

Reputation: 21144

Implementing NSFetchedResultsSectionInfo

I have fairly large amount of data which I would like to partition into the custom sections, for sections based on some keypath and some other sections on some other conditions. So I came up with idea to subclass NSFetchedResultsController. This subclass of NSFetchedResultsController should provide the flexibility of returning the sections in its method -(NSArray*)sections which returns an array of id<NSFetchedResultsSectionInfo>. If I could implement by own class that conforms to the protocol and then pass the custom array of the objects in sections method of fetchedResultsController, I think I could accomplish my task.

Is there a way to implement NSFetchedResultsSectionInfo in your own class and then return the custom array of such object in the fetchedResultsController subclass? Or is there some library to accomplish this?

Edit

Let me describe what I am trying to achieve. I have Contacts model as NSManagedObject subclass. This entity has a attribute called state which can be unknown, known and unidentified. Now, I want these contacts to be sectioned in such a way that all the unknown contact appear in a single section, also the unidentified contacts should be in another different section. Now, all other known contacts should be in the different sections according to the name of contact. How can this be done ?

My table view should be populated as;

Unknown Contacts (sorted into a single section according to date )
  - 
  -
Known Contacts(A)(known contacts sorted into different sections according to the name)
  -
  -
Known Contacts(B)
  -
  -
  -
Known Contacts(C)
  -
  -
  -
  -
  -
  -
Known Contacts(Z)
 -
UnIdentified Contacts(sorted into a single section with first name)
  -
  -
  -

Upvotes: 0

Views: 1587

Answers (1)

Mundi
Mundi

Reputation: 80265

NSFetchedResultsController already implements this.

NSArray* sectionsArray = self.fetchedResultsController.sections;

To implement the special sections follow the principles in the Apple sample code DateSectionTitles. The pattern is as follows:

  • Create a new transient attribute sectionIdentifier. Like in the sample code, also include the primitiveSectionIdentifier attribute in your managed object subclass. This is the attribute that you use for sectionNameKeyPath in your FRC.

  • Devise a scheme that sorts correctly. Put all the sorting and computing logic into the class implementation file's custom getter for sectionIdentifier. For example you could simply use two characters that sort before the capital letter "A" which presumably would be the first of your regular section headers. You could also make it into a number, say 1 and 2 for your first two sections, and 3+ for the first letters in the alphabet.

  • Decode your scheme in titleForHeaderInSection to display the desired content.

I recently did this for a client who had a non-standard coding scheme for a large list of contacts indicating some marketing segmentation. The data for the section identifier was also pulled from several different attributes.

Upvotes: 1

Related Questions