ios - Core-Data NSFetchedResultsController to-many relationship -
here datamodel: datamodel http://s1.directupload.net/images/131007/ud8fqkra.png
i filled database data.
now want display in tableview. first want select day 'days' attribute, lessons. want display attributes 'end' , 'start' in header (-(nsstring *) tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section
)
then want display lesson data in right section. how can nsfetchedresultscontroller?
i've set basic code:
nsfetchrequest* fetch = [nsfetchrequest fetchrequestwithentityname:nsstringfromclass([lessons class])]; nssortdescriptor* sortgroup = [nssortdescriptor sortdescriptorwithkey:@"lesson" ascending:yes]; fetch.sortdescriptors = @[sortgroup]; nsfetchedresultscontroller *controller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetch managedobjectcontext:self.managedobjectcontext sectionnamekeypath:nil cachename:nil]; nserror* error; [controller performfetch:&error]; if (error) { nslog(@"error: %@", error); abort(); }
edit:
here storyboard:
update
here's code:
tableview.m (important methods)
-(id)init { self = [super init]; if (self) { nslog(@"%s",__pretty_function__); self.del = [[uiapplication sharedapplication] delegate]; self.managedobjectcontext = self.del.managedobjectcontext; nsfetchrequest* fetch = [nsfetchrequest fetchrequestwithentityname:@"lessons"]; nsstring *theselectedday = @"mi"; nspredicate *pred = [nspredicate predicatewithformat:@"lessontoday.day == %@", theselectedday]; fetch.predicate = pred; nssortdescriptor *sorttime = [nssortdescriptor sortdescriptorwithkey:@"lessontotime.start" ascending:yes]; //nssortdescriptor *sortgroup = [nssortdescriptor sortdescriptorwithkey:@"lesson" ascending:yes]; fetch.sortdescriptors = @[sorttime]; self.controller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetch managedobjectcontext:self.managedobjectcontext sectionnamekeypath:@"lessontotime.start" cachename:nil]; } return self; } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return 1; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; // configure cell... //uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } lessons *lesson = [self.controller objectatindexpath:indexpath]; cell.textlabel.text = lesson.lesson; return cell; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { id <nsfetchedresultssectioninfo> sectioninfo = [[self.controller sections] objectatindex:section]; // first lesson in section: lessons *lesson = [sectioninfo objects][0]; //for testing changed type of 'start' , 'end' string nslog(@"start: %@",lesson.lessontotime.start); nslog(@"end: %@", lesson.lessontotime.end ); nsstring *title = [nsstring stringwithformat:@"%@-%@", lesson.lessontotime.start, lesson.lessontotime.end]; return title; }
result:
first of have add predicate lessons selected day displayed:
nspredicate *pred = [nspredicate predicatewithformat:@"lessontoday.day == %@", theselectedday]; fetch.predicate = pred;
to group table view sections, have set sectionnamekeypath:
parameter of fetched results controller, example @"lessontotime.start"
. group lessons same start time 1 section. (as understand comment, lessons same start time have same end time, should sufficient.) same key path must used first sort descriptor:
nssortdescriptor *sorttime = [nssortdescriptor sortdescriptorwithkey:@"lessontotime.start" ascending:yes]; nssortdescriptor *sortgroup = [nssortdescriptor sortdescriptorwithkey:@"lesson" ascending:yes]; fetch.sortdescriptors = @[sorttime, sortgroup]; nsfetchedresultscontroller *controller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetch managedobjectcontext:self.managedobjectcontext sectionnamekeypath:@"lessontotime.start" cachename:nil];
to display section header according needs, have override titleforheaderinsection:
:
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { id <nsfetchedresultssectioninfo> sectioninfo = [[self.controller sections] objectatindex:section]; // first lesson in section: lesson *lesson = [sectioninfo objects][0]; // build title lesson.lessontotime.start , lesson.lessontotime.end: nsstring *title = ...; return title; }
Comments
Post a Comment