Ray
Ray

Reputation: 25

Separate the fetch data from core data in different UITableView's sections

Say I have:date, food, amount attributes in my "Record" entity

I fetch the data from my db (core data), I've set the predicate to only fetch the data to specific month.

How can I display them in UITableView like the following:

---July 1----

cell

cell

---July 2----

cell

cell

cell

Upvotes: 0

Views: 299

Answers (2)

Mundi
Mundi

Reputation: 80265

Your attribute is of type NSDate so every date will be unique and result in a separate section. This is clearly not desired.

You need to follow the example in the Apple Sample code DateSectionTitles. Essentially, you create a transient string object with your managed object subclass that represents the date. You can then use that attribute as the sectionNameKeyPath of your fetched results controller.

Upvotes: 1

Wilko X
Wilko X

Reputation: 340

[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:managedObjectContext
                                      sectionNameKeyPath:@"YOUR SECTION KeyPath"
                                               cacheName:nil];

Just fill in the name of the attribute containing the date for the sectionNameKeyPath.

Upvotes: 0

Related Questions