uitableview - NSFetchedResultsController: ReOrder Sections -
i'm next illustration apple setup sections:
https://developer.apple.com/library/ios/samplecode/datesectiontitles/listings/datesectiontitles_aplevent_m.html
my sections appear in next order:
section 0: "upcoming" section 1: "today" section 2: "past"
code utilize in nsmanagedobject
.m file:
#pragma mark - transient properties - (nsstring *)sectionidentifier { // create , cache section identifier on demand. [self willaccessvalueforkey:@"sectionidentifier"]; nsstring *tmp = [self primitivesectionidentifier]; [self didaccessvalueforkey:@"sectionidentifier"]; if (!tmp) { nsdate *datetocompare = [self getutcformatedate:[self startdate]]; nslog(@"********date compare****** %@", datetocompare); nscalendar* calendar = [nscalendar currentcalendar]; nsdate* = [nsdate date]; nsdateformatter *format = [[nsdateformatter alloc] init]; format.dateformat = @"dd-mm-yyyy"; nsstring *stringdate = [format stringfromdate:now]; nsdate *todaysdate = [format datefromstring:stringdate]; nsinteger differenceindays = [calendar ordinalityofunit:nsdaycalendarunit inunit:nseracalendarunit fordate:datetocompare] - [calendar ordinalityofunit:nsdaycalendarunit inunit:nseracalendarunit fordate:todaysdate]; nsstring *sectionstring; if (differenceindays == 0) { sectionstring = ksectionidtoday; } else if (differenceindays < 0) { sectionstring = ksectionidpast; } else if (differenceindays > 0) { sectionstring = ksectionidupcoming; } tmp = sectionstring; [self setprimitivesectionidentifier:tmp]; } homecoming tmp; } -(nsdate *)getutcformatedate:(nsdate *)localdate { nsdateformatter *dateformatter; if (!dateformatter) { dateformatter = [[nsdateformatter alloc] init]; } nstimezone *timezone = [nstimezone timezonewithname:@"utc"]; [dateformatter settimezone:timezone]; [dateformatter setdateformat:@"yyyy-mm-dd"]; nsstring *datestring = [dateformatter stringfromdate:localdate]; nsdate *datefromstring = [[nsdate alloc] init]; datefromstring = [dateformatter datefromstring:datestring]; homecoming datefromstring; } #pragma mark - time stamp setter - (void)setstartdate:(nsdate *)newdate { // if time stamp changes, section identifier become invalid. [self willchangevalueforkey:@"startdate"]; [self setprimitivestartdate:newdate]; [self didchangevalueforkey:@"startdate"]; [self setprimitivesectionidentifier:nil]; } #pragma mark - key path dependencies + (nsset *)keypathsforvaluesaffectingsectionidentifier { // if value of timestamp changes, section identifier may alter well. homecoming [nsset setwithobject:@"startdate"]; }
in tableviewcontroller, setup nsfetchedresults following:
- (nsfetchedresultscontroller *)fetchedresultscontroller { if(_fetchedresultscontroller!=nil) { homecoming _fetchedresultscontroller; } nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"entity" inmanagedobjectcontext:self.managedobjectcontext]; [fetchrequest setentity:entity]; nssortdescriptor *firstsort = [[nssortdescriptor alloc] initwithkey:@"startdate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc]initwithobjects:firstsort,nil]; [fetchrequest setsortdescriptors:sortdescriptors]; self.fetchedresultscontroller = [[nsfetchedresultscontroller alloc]initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:@"sectionidentifier" cachename:nil]; self.fetchedresultscontroller.delegate = self; homecoming self.fetchedresultscontroller; }
question 1: how sections appear in next order:
section 0: today section 1: upcoming section 2: past
question 2: within each section, how sort rows based on attribute called "modified"
in each object?
both section , row ordering 100% dependent upon sort descriptors. want first sort descriptor sort proper section , next sort descriptors sort rows within sections.
for example, if wanted 3 sections based off of "group" , wanted rows sorted name within of grouping add together sort descriptors as:
nsarray *descriptors = @[[nssortdescriptor sortdescriptorwithkey:@"group" ascending:yes], [nssortdescriptor sortdescriptorwithkey:@"name" ascending:yes]]; [fetchrequest setsortdescriptors:descriptors];
your section key nsfetchedresultscontroller
need match first nssortdescriptor
.
uitableview core-data ios7 nsfetchedresultscontrolle
No comments:
Post a Comment