Display data in table view -
i have created 1 application core data unable display data in table view. writing code below have written fetching results:
#import "rootviewcontroller.h" #import "detailviewcontroller.h" #import "addviewcontroller.h" #import "employeedetailsappdelegate.h" /* template not ensure user interface consistency during editing operations in table view. must implement appropriate methods provide user experience require. */ @interface rootviewcontroller () - (void)configurecell:(uitableviewcell *)cell atindexpath:(nsindexpath *)indexpath; @end @implementation rootviewcontroller @synthesize detailviewcontroller, fetchedresultscontroller, managedobjectcontext; @synthesize array; #pragma mark - #pragma mark view lifecycle - (void)viewdidload { // employeedetailsappdelegate *appdelegate = [[uiapplication sharedapplication]delegate]; self.title = @"employee name"; self.navigationitem.rightbarbuttonitem = self.editbuttonitem; nsmutablearray *temparr = [[nsmutablearray alloc]initwithcapacity:0]; if (self.fetchedresultscontroller) { temparr = self.fetchedresultscontroller.fetchedobjects; } self.array = temparr; [super viewdidload]; [temparr release]; } - (void)viewwillappear:(bool)animated { [self.tableview reloaddata]; [super viewwillappear:animated]; } /* - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; } */ /* - (void)viewwilldisappear:(bool)animated { [super viewwilldisappear:animated]; } */ /* - (void)viewdiddisappear:(bool)animated { [super viewdiddisappear:animated]; } */ - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // ensure view controller supports rotation , split view can therefore show in both portrait , landscape. return yes; } #pragma mark - #pragma mark add new object - (void)insertnewobject:(id)sender { addviewcontroller *add = [[addviewcontroller alloc]initwithnibname:@"addviewcontroller" bundle:nil]; self.modalpresentationstyle = uimodalpresentationformsheet; add.wantsfullscreenlayout = no; [self presentmodalviewcontroller:add animated:yes]; [add release]; } #pragma mark - #pragma mark table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if([self.array count]) { return [array count]; } return 1; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; } // configure cell. [self configurecell:cell atindexpath:indexpath]; return cell; } -(void)configurecell:(uitableviewcell *)cell atindexpath:(nsindexpath *)indexpath{ addviewcontroller *detail = [fetchedresultscontroller objectatindexpath:indexpath]; cell.textlabel.text = detail.empname.text; } - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // delete managed object. nsmanagedobject *objecttodelete = [self.fetchedresultscontroller objectatindexpath:indexpath]; if (self.detailviewcontroller.detailitem == objecttodelete) { self.detailviewcontroller.detailitem = nil; } nsmanagedobjectcontext *context = [fetchedresultscontroller managedobjectcontext]; [context deleteobject:[fetchedresultscontroller objectatindexpath:indexpath]]; nserror *error; if (![context save:&error]) { /* replace implementation code handle error appropriately. abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. if not possible recover error, display alert panel instructs user quit application pressing home button. */ nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } } } - (bool)tableview:(uitableview *)tableview canmoverowatindexpath:(nsindexpath *)indexpath { // table view should not re-orderable. return no; } #pragma mark - #pragma mark table view delegate - (void)tableview:(uitableview *)atableview didselectrowatindexpath:(nsindexpath *)indexpath { // set detail item in detail view controller. detailviewcontroller = [[detailviewcontroller alloc]initwithstyle:uitableviewstyleplain]; addviewcontroller *selectedname = (addviewcontroller *)[[self fetchedresultscontroller]objectatindexpath:indexpath]; detailviewcontroller.detail = selectedname; [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; [detailviewcontroller release]; } #pragma mark - #pragma mark fetched results controller - (nsfetchedresultscontroller *)fetchedresultscontroller { if (fetchedresultscontroller != nil) { return fetchedresultscontroller; } /* set fetched results controller. */ // create fetch request entity. nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; // edit entity name appropriate. nsentitydescription *entity = [nsentitydescription entityforname:@"details" inmanagedobjectcontext:managedobjectcontext]; [fetchrequest setentity:entity]; // set batch size suitable number. [fetchrequest setfetchbatchsize:20]; // edit sort key appropriate. nssortdescriptor *namedescriptor = [[nssortdescriptor alloc] initwithkey:@"employeename" ascending:no]; nssortdescriptor *iddescriptor = [[nssortdescriptor alloc] initwithkey:@"employeeid" ascending:no]; nsmutablearray *sortdescriptors = [[nsmutablearray alloc] initwithobjects:namedescriptor, iddescriptor, nil]; self.array = sortdescriptors; [fetchrequest setsortdescriptors:sortdescriptors]; // edit section name key path , cache name if appropriate. // nil section name key path means "no sections". nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:managedobjectcontext sectionnamekeypath:nil cachename:@"root"]; afetchedresultscontroller.delegate = self; self.fetchedresultscontroller = afetchedresultscontroller; [afetchedresultscontroller release]; [fetchrequest release]; [namedescriptor release]; [sortdescriptors release]; return fetchedresultscontroller; } #pragma mark - #pragma mark fetched results controller delegate - (void)controllerwillchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview beginupdates]; } - (void)controller:(nsfetchedresultscontroller *)controller didchangesection:(id <nsfetchedresultssectioninfo>)sectioninfo atindex:(nsuinteger)sectionindex forchangetype:(nsfetchedresultschangetype)type { switch(type) { case nsfetchedresultschangeinsert: [self.tableview insertsections:[nsindexset indexsetwithindex:sectionindex] withrowanimation:uitableviewrowanimationfade]; break; case nsfetchedresultschangedelete: [self.tableview deletesections:[nsindexset indexsetwithindex:sectionindex] withrowanimation:uitableviewrowanimationfade]; break; } } - (void)controller:(nsfetchedresultscontroller *)controller didchangeobject:(id)anobject atindexpath:(nsindexpath *)indexpath forchangetype:(nsfetchedresultschangetype)type newindexpath:(nsindexpath *)newindexpath { uitableview *tableview = self.tableview; switch(type) { case nsfetchedresultschangeinsert: [tableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationfade]; break; case nsfetchedresultschangedelete: [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; break; case nsfetchedresultschangeupdate: [self configurecell:[tableview cellforrowatindexpath:indexpath] atindexpath:indexpath]; break; case nsfetchedresultschangemove: [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; [tableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath]withrowanimation:uitableviewrowanimationfade]; break; } } - (void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview endupdates]; } #pragma mark - #pragma mark memory management - (void)didreceivememorywarning { // releases view if doesn't have superview. [super didreceivememorywarning]; // relinquish ownership cached data, images, etc. aren't in use. } - (void)viewdidunload { self.fetchedresultscontroller = nil; // relinquish ownership of can recreated in viewdidload or on demand. // example: self.myoutlet = nil; self.array = nil; } - (void)dealloc { [array release]; [detailviewcontroller release]; [fetchedresultscontroller release]; [managedobjectcontext release]; [super dealloc]; } @end
the problem getting warning following line:
temparr = self.fetchedresultscontroller.fetchedobjects;
the warning is: "/users/satyam/desktop/employeedetails/classes/rootviewcontroller.m:43:0 /users/satyam/desktop/employeedetails/classes/rootviewcontroller.m:43: warning: incompatible objective-c types assigning 'struct nsarray *', expected 'struct nsmutablearray *'"
please me.
thanks , regards, dipanjan
just read warning word word.
you try assign nsarray variable expects nsmutablearray.
self.fetchedresultscontroller.fetchedobjects nsarray
temparr nsmutablearray
but if use nsfetchedresultscontroller don't need own data array @ all. nsfetchedresultscontroller handles data you.
so maybe want replace
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if([self.array count]) { return [array count]; } return 1; }
with
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { id <nsfetchedresultssectioninfo> sectioninfo = [[self.fetchedresultscontroller sections] objectatindex:section]; return [sectioninfo numberofobjects]; }
your own datasource invalid anyway. because assign sortdescriptor used nsfetchedresultscontroller it.
nsmutablearray *sortdescriptors = [[nsmutablearray alloc] initwithobjects:namedescriptor, iddescriptor, nil]; self.array = sortdescriptors;
i don't know wanted achieve doing this.
Comments
Post a Comment