objective c - Populating ViewBased table from data in 6 different NSMutable arrays -
i have 6 different nsmutable arrays [(arr(a); arr(b); etc.] each of represents data 1 cell of row in table. array key representation. built in prior classes, passed current class , have same number of string objects (63) in each. table essential data representation of
questions:
must build
nsdictionary'keys' , 'objects'. how buildthatdictionary. have tried various methods stepping through count loop no success. can find method inserting objects specific keys.can load table directly various arrays without
viewcontroller. have table , id's laid out have not been able addviewcontrollerclass - have 'files owner'. if easiest how can that.if needed, how take elements of 6 arrays in order comma delimited string in array becomes input row of of table , therefore parsed table.
no code offer because attempts w/code have been unsuccessful.
need specific direction here.
.h
// reportsoutput.h // stamp collection // created terry lengel on 4/20/15. // copyright (c) 2015 terry lengel. rights reserved. #import <cocoa/cocoa.h> #import <foundation/foundation.h> #import "reportsclass.h" @interface reportsoutput : nswindowcontroller <nsmenudelegate,nstableviewdatasource,nstableviewdelegate,nsapplicationdelegate>{ // variable , outlet table iboutlet nstableview *rpttable; } // data element sources @property(nonatomic,strong) nsmutablearray *tblyrscott; @property(nonatomic,strong) nsmutablearray *tblyrext; @property(nonatomic,strong) nsmutablearray *tblyryear; @property(nonatomic,strong) nsmutablearray *tblyrtype; @property(nonatomic,strong) nsmutablearray *tblyrprice; @property(nonatomic,strong) nsmutablearray *tblyrdescription; // display data source array table @property(strong) nsmutablearray *rptdata; #pragma mark - method declarations -(bool)conditiondata; -(nsdictionary *)makedictionaryrecord:(nsstring*)scott withinfo:(nsstring*)ext withinfo:(nsstring*)year withinfo:(nsstring*)type withinfo:(nsstring*)price withinfo:(nsstring*)description; @end .m // reportsoutput.m // stamp collection // created terry lengel on 4/20/15. // copyright (c) 2015 terry lengel. rights reserved. #import "reportsoutput.h" #import "reportsclass.h" @interface reportsoutput () @end @implementation reportsoutput @synthesize tblyrscott; @synthesize tblyrext; @synthesize tblyrtype; @synthesize tblyrprice; @synthesize tblyryear; @synthesize tblyrdescription; @synthesize rptdata; -(id)initwithwindow:(nswindow *)window{ self = [super initwithwindow:window]; if (self){ // initialize code here } return self; } -(void)windowdidload { [super windowdidload]; // implement method handle initialization after window controller's window has been loaded nib file. } -(void)applicationdidfinishlaunching:(nsnotification *)notification{ //insert code here initialize application } // terminate app using red button: -(bool)applicationshouldterminateafterlastwindowclosed: (nsapplication *)sender{ return yes; } -(void)awakefromnib{ if (self.conditiondata == yes){ [rpttable reloaddata]; } } -(bool)windowshouldclose:(id)sender{ return yes; } -(void)performclose:(id)sender{ [self close]; } #pragma mark - table view data source -(nsinteger)numberofrowsintableview:(nstableview *)tableview{ return rptdata.count; } -(nsview *) tableview:(nstableview *)tableview viewfortablecolumn:(nstablecolumn *)tablecolumn row:(nsinteger)row{ nstablecellview *scott = [tableview makeviewwithidentifier:@"scott" owner:self]; scott.textfield.stringvalue = [self.rptdata objectatindex:row]; return scott; } // request sorting -(void) tableview:(nstableview *)tableview sortdescriptorsdidchange:(nsarray *)olddescriptors{ // table view received sort request //sort data, reload tableview data: [rptdata sortusingdescriptors:[rpttable sortdescriptors]]; [rpttable reloaddata]; } -(bool)conditiondata{ bool conditiondata = no; nsstring *rprice; nsstring *rext; nsstring *ryear; nsstring *rtype; nsstring *rdescription; (int b=0; b<[tblyrscott count]; ++b){ //condition source data remove null appearences rext = [tblyrext objectatindex:b]; if (rext == (id)[nsnull null] || rext.length == 0 ){ rext = @"none"; }else if ([rext isequaltostring:@" "]){ rext = @"none"; }else rext = [tblyrext objectatindex:b]; ryear = [tblyryear objectatindex:b]; if (ryear == (id)[nsnull null] || ryear.length == 0 ){ ryear = @" "; }else ryear = [tblyryear objectatindex:b]; rtype = [tblyrtype objectatindex:b]; if (rtype == (id)[nsnull null] || rtype.length == 0 ){ rtype = @" "; }else rtype = [tblyrtype objectatindex:b]; rprice = [tblyrprice objectatindex:b]; if (rprice == (id)[nsnull null] || rprice.length == 0 ){ rprice = @"n/r"; }else rprice = [tblyrprice objectatindex:b]; rdescription = [tblyrdescription objectatindex:b]; if (rdescription == (id)[nsnull null] || rdescription.length == 0 ){ rdescription = @" "; }else rdescription = [tblyrdescription objectatindex:b]; nsdictionary *rptdata = @{@"scott":[tblyrscott objectatindex:b],@"ext":rext,@"year":ryear,@"type":rtype,@"price":rprice,@"description":rdescription}; } //[rpttable reloaddata]; return conditiondata = yes; } -(nsdictionary*) makedictionaryrecord:(nsstring *)scott withinfo: (nsstring *)ext withinfo: (nsstring *)year withinfo: (nsstring *)type withinfo: (nsstring *)price withinfo: (nsstring *)description{ nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:scott,@"scott",ext,@"ext",year,@"year",type,@"type",price,@"price",description,@"description", nil]; return dict; } @end data sample: 2015-04-25 12:20:49.251 stampsprojectdev[2981:591183] rptdata = { description = "lunar new year - horse"; ext = none; price = "n/r"; scott = 4846; type = c; year = 2014; } 2015-04-25 12:20:49.252 stampsprojectdev[2981:591183] rptdata = { description = "jimi hendrix"; ext = none; price = "n/r"; scott = 4880; type = c; year = 2014; } 2015-04-25 12:20:49.252 stampsprojectdev[2981:591183] rptdata = { description = "charlton heston"; ext = none; price = "n/r"; scott = 4892; type = c; year = 2014; } 2015-04-25 12:20:49.252 stampsprojectdev[2981:591183] rptdata = { description = "janice joplin"; ext = none; price = "n/r"; scott = 4916; type = c; year = 2014; } 2015-04-25 12:20:49.252 stampsprojectdev[2981:591183] rptdata = { description = "ralph ellison"; ext = none; price = "n/r"; scott = 4866; type = c; year = 2014; }
must build nsdictionary 'keys' , 'objects'. how build thatdictionary. have tried various methods stepping through count loop no success. can find method inserting objects specific keys.
not required, advisable.
// assume 6 arrays array0, array1, ... // assume array0.count == array1.count == etc (int i=0; i<array0.count; ++i) { nsdictionary *di = @{ @"ar0":array0[i], @"ar1":array1[i],... }; } but better idea nsobject subclass create meaningful users , has properties corresponding elements arrays (call meaningfulobject)....
// in datasource's interface definition @property(strong) nsmutablearray *myarrayofmeaningfulobjects; self.myarrayofmeaningfulobjects = [@[] mutablecopy]; // assume array0.count == array1.count == etc (int i=0; i<array0.count; ++i) { meaningfulobject *mi = [meaningfulobject meaningfulobjectwithattribute0:array0[i] attribute1:array1[i]... ]; [self.myarrayofmeaningfulobjects addobject:mi]; } can load table directly various arrays without viewcontroller. have table , id's laid out have not been able add viewcontroller class - have 'files owner'. if easiest how can that.
the table loads itself. job provide datasource, array of objects representing rows. people opt make view controller who's view contains table act datasource, table's datasource can object. 2 required parts of nstableviewdatasource protocol (1) numberofrowsintableview: this:
return self.myarrayofmeaningfulobjects.count; and (2) tableview:viewfortablecolumn:row: this:
meaningfulobject *mrow = self.myarrayofmeaningfulobjects[indexpath.row]; // configure tableview cell using mrow's attributes cell.textlabel.text = mrow.attribute0; if needed, how take elements of 6 arrays in order comma delimited string in array becomes input row of of table , therefore parsed table.
don't understand one, answer implicit in foregoing
edit, upon review of code, seems me you've got it. code has declared array basis of datasource, this:
@property(strong) nsmutablearray *rptdata; good, consequential mistake made here:
nsdictionary *rptdata = @{@"scott":[tblyrscott objectatindex:b],@"ext":rext,@"year":ryear,@"type":rtype,@"price":rprice,@"description":rdescription}; that's dictionary looks if represents single row in table, named confusingly nsarray property. looks if dictionary built abandoned on iteration of loop, overwritten object next row. make work, objects representing rows must added datasource, this:
// renamed rptdata dictionary rowdictionary nsdictionary *rowdictionary = @{@"scott":[tblyrscott objectatindex:b],@"ext":rext,@"year":ryear,@"type":rtype,@"price":rprice,@"description":rdescription}; [self.rptdata addobject:rowdictionary];
Comments
Post a Comment