swift - NSFetchedResultsController different sort descriptors for each section -
i have objects nsdate property , need split them 2 sections (first - future events, second - historic events) , first section need sort them date property in ascending order , second section in descending order. idea how sorting?
assuming using nsfetchedresultscontroller
, underlying fetch must sorted 1 way or other. can think of 2 different solutions:
use 2 separate frcs, complementary predicates 1 handles past events, while other handles future events. 1 sorted ascending, other descending. problem both frcs generate
indexpaths
section 0. therefore need remap second frc'sindexpaths
use section 1 of tableview. example, incellforrowatindexpath
need this:if (indexpath.section == 0) { objecttodisplay = self.futurefetchedresultscontroller.objectatindexpath(indexpath) } else { // use second frc let frcindexpath = nsindexpath(forrow: indexpath.row, insection: 0) objecttodisplay = self.pastfetchedresultscontroller.objectatindexpath(frcindexpath) }
alternatively, stick single frc, sorted ascending. remap
indexpath
second section, last object in section displayed in row 0, etc:if (indexpath.section == 0) { objecttodisplay = self.fetchedresultscontroller.objectatindexpath(indexpath) } else { // use remap reverse sort order frc let sectioninfo = self.fetchedresultscontroller.sections[1] as! nsfetchedresultssectioninfo let sectioncount = sectioninfo.numberofobjects let frcindexpath = nsindexpath(forrow: (sectioncount - 1 - indexpath.row), insection:indexpath.section) objecttodisplay = self.fetchedresultscontroller.objectatindexpath(frcindexpath) }
personally think second option preferable. in each case, tableview datasource/delegate methods need same remapping, , frc delegate methods need reverse mapping.
Comments
Post a Comment