TableView Sections based on dates

I have been through the CoreData by Tutorials, and this deals with section headings in a table view which already exist in the Entity attribute, which makes it easy to put into the correct section.

I’m looking for information on how to go about creating sections depending on dates contained in CoreData entities.

For example, how to create 3 sections “Last Year”, “This Year” and “Next Year” and add the results from NSFetchedResultsController into the appropriate sections.

Has anyone done this?

I can I suggest an other solution:

  • fetch from you db the That want display in tableview
  • create 3 array selecting in base of data. So you can have an array with ‘Last Year’, ‘This Year’ and ‘Next Year’.
  • put these arrays in an other array.

in the numberOfSection method return the count of last array created. es. last array’s name is ‘listSection’ you return listSection.count, understand?

in the numberOfRowsInSection method:
[[listSection objectAtIndex:indexPath.section] count] //Objective-C version
let section = listSection[indexPath.section] //Swift version
return section.count //swift version

in few word, get the array from listSection in base the section and count the array of section.

I hope I have helped you

Thanks rufy, I think I understand what you mean.

Create an array to hold objects relating to “Last Year”
Create a second array to hold objects relating to “This Year”
Create a third array to hold objects relating to “Last Year”

Fetch objects from the required Entity and add objects to the relevant array.

Put these 3 arrays into another array called “sectionList”

In the numberOfSection method:
return sectionList.count

In the numberOfRowsInSection method:
let section = sectionList[indexPath.section]
return section.count

I’m using CoreData and Swift. I’ll give this a try and see how it goes…